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 Harnessing the Power of Objects Constants

Why don't you change the MAX_PEZ variable from public final to private final?

Why don't you change the MAX_PEZ variable from public final to private final? Wouldn't it be the same, but harder to change?

2 Answers

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello Tianerad 999 .

Sorry, I have not watched the video but we have something we call Encapsulation in Java whereby we declare class variables as private (only accessible within the same class). This is to make sure that "sensitive" data is hidden from clients (users) using the variables.

For example, if MAX_PEZ variable was public then anyone or any class can make unauthorized changes to the value of MAX_PEZ resulting in a corrupted program. So making the variable private limits access to this variable to be within the class containing MAX_PEZ variable.

Try and imagine if an unauthorized class changes MAX_PEZ variable to 1,000,000.00, our program will be broken completely and won't function well.

Oh, that makes sense. Thanks for helping!