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

kabirdas
kabirdas
1,976 Points

Using public access method with final vs privat

In this section, we're adding the code

public final into MAX_PEZ = 12;

so that after it is set to 12, it can't be changed again. Why wouldn't we just set it to private?

3 Answers

kabirdas
kabirdas
1,976 Points

I understand that. But what difference would:

private into MAX_PEZ = 12;

have?

Edward Shirinian
Edward Shirinian
1,446 Points

Short version: Private does not allow other classes to access your variables, methods etc. only other variables, methods etc. in your class can access it. If you use final, your class and other classes can access it, but they cannot change the value of your variables, methods etc.

Long version:

Imagine you're working with a team of developers. You created a class with a method XYZ() that uses variable MAX_PEZ = 12; . MAX_PEZ must not be changed to make your program work properly. These are called constants. When declaring them they must be in all caps and have an underscore separating the words.

(When I say they must be in all caps I'm really saying that it's best practice so when other people read your code, they'll know its a constant and not a normal variable)

Another member named "K" is assigned to the same project and needs to work on the class you wrote. "K" doesn't bother reading your notes and disregards the capitalized letters of MAX_PEZ. They make another method HIJ() that tries to change the value of MAX_PEZ. If the final keyword is there, then they wont be able to change it and will get a compilation error. Your program is safe! Let's assume that you've already added final to MAX_PEZ.

"K" then gets assigned to to make another class that interacts with your class. "K" is kind of lazy and doesn't want to declare a whole bunch of variables all over again. The values of the variables you declared in your class are the same values that "K" needs. So "K" decides to use MAX_PEZ in their class.

Some time goes by.......

A new decision comes in and the variables need to be changed in your class to add a new feature to the program. The value of MAX_PEZ gets changed from 12 to 16. (To be clear MAX_PEZ does not get changed by another method or class. MAX_PEZ got changed where it was declared in your original class) The program runs, bit it's not running as intended.

Why? What happened?

When MAX_PEZ got changed in your class it also got changed in "K"'s class as well. "K"'s class relied on MAX_PEZ being 12 to run properly. Now that MAX_PEZ is 16, the output of "K"'s class has changed.

Unfortunately these errors are sometimes not easily picked up on. Now the developers have to go through potentially 100's of classes and 1000's of lines of code to find the error. This uses up a lot of the companies time and money.

If the variable AB_C was declared as private from the start, there would never have been this problem. As a programmer it is a best practice to only give access to the parts of your program that is relevant for the functionality of the whole program. This is called separating the concerns.

If you rely on AB_C to make your class work the way it should, then make AB_C accessible to that class and that class alone.

Over time as you gain more experience you'll start to see where final and private should be used, but I hope this was a good enough example for you to understand the concept.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

public and private are access modifiers and have no bearing on the ability to change a constant or point it to another object. the usual declaration syntax is public static final (type) NAME_IN_ALL_CAPS.