Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Mahvish Irfan
Mahvish Irfan
449 Points

Why did we make the mCharacterName private but the MAX_PEZ public?

I don't get it

4 Answers

Ryan Chan
Ryan Chan
4,076 Points

The "private" keyword allows you to access your variable inside the class but not outside of it.

Thus, mCharacterName is private, because we don't want to modify it outside of its class, but still want to access it inside the PezDispenser class.

On the other hand, adding the "final" keyword makes it so you can't access it inside the class nor outside of it.

Thus, we can skip adding a getter and just make it public, since it doesn't allow us to modify the value anywhere.

I hope this clears up your question, if not just ignore what I said.

-Ryan

Mahvish Irfan
Mahvish Irfan
449 Points

Ryan Ruscett I still don't understand :/

Also, is there a way I can upload a screenshot of my code so that way you can see it?

Ryan Ruscett
Ryan Ruscett
23,309 Points

Max pez needs to be available to other classes so that other classes can update the amount of pez there are. The name however is private, because it should never be updated. It belongs to that class, and can not be changed.

If you provide a code example I can explain exactly what you don't understand better.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Max pez needs to be available to other classes so that other classes can update the amount of pez there are. The name however is private, because it should never be updated. It belongs to that class, and can not be changed.

If you provide a code example I can explain exactly what you don't understand better.