Java Java Objects Harnessing the Power of Objects Filling the Dispenser

Stephen Crispini
PRO
Stephen Crispini
Pro Student 619 Points

Why make MAX_PEZ public, but characterName private?

I recall that we made characterName private so that it couldn't be directly accessed or changed (we don't want Yoda becoming Vader after all). But it seems like setting MAX_PEZ to public and final accomplishes this too: it can have a variable assigned to it once and then that can't be changed, but it's directly accessible. So I guess I'm curious why characterName should be private, since it then requires an additional method "GetCharacterName" to return the name versus saying return characterName right?

2 Answers

andren
andren
28,405 Points

The reason why characterName is not simply declared with public is that at a later point in this course you will actually be removing the final keyword and adding a method to the class that does end up changing the characterName field. In order to force a user to only change the field by calling that method the field has to be private.

Making a field private and then adding one method to get the field and one method to set the field (often referred to as a getter and a setter) is an extremely common design pattern. It is an example of Encapsulation which is often considered to be a fundamental part of object-oriented programming.

Part of the advantage it holds over just making the field public is that it gives you control over what happens when the field is accessed and control over what happens when it is changed. As well as the ability to control what values the field is changed to. And that means you don't have to worry about a field suddenly having it's value changed at a time or in a way that the class was not designed to handle.

Edit: Changed some of this post as I had misremembered a part of this course.

Stephen Crispini
Stephen Crispini
Pro Student 619 Points

Ooh okay that makes a lot more sense to me now! Whereas the MAX_PEZ will remain a constant, so there's no need to wall it off because it is set in stone once and will never be changed.

I think I was confused because earlier in the lesson, he talked about the characterName as if it would be set once and never changed again. Hadn't thought about the fact that further functionality would be added down the line that would change that. Thanks so much.

Yanuar Prakoso
Yanuar Prakoso
15,186 Points

Hi Stephen..

The shortest answer for your case is: the key is in the final word in the public final for MAX_PEZ constant. Since MAX_PEZ is a constant the value will be fixed through out the program when it runs. However, characterName is a variable which the user of your program will decide who will be the characterName of their own Pez. If you coose to use public final on characterName variable your users will not be able to change this as they wanted.

You mentioned the getCharacterName method in your question, which commonly known in Java programming as getter method. In pair with the getter it was obvious there will be setter method. In your case the setter is in the construction method with this.characterName variable which received the characterName value chosen by user when it called PezDispenser class. You must give characterName right when you call the new PezDispenser class right. If the characterName variable is set to be public final you will not be able to do this.

I guess you already familiar with encapsulation and/or abstraction features and what it importance for best practice in object oriented programming since you mentioned "you do not want anyone to change the characterName". That is exactly why need to make variables private unless it is really needed to be public. The encapsulation will avoid any user to make mistakes inputting values that will crash the program. When user input something it must be validated and also when the user ask to get something it must also be validated that is what getter and setter method is for actually. You will learn this more in the future courses in Java.

I hope this will help. Any further addition to the explanation will be much appreciated. Keep on coding. If you had more question on this just post it more.