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) Meet Objects Creating Classes

Dylan Carter
Dylan Carter
4,780 Points

Making a class or variable Public vs. Private?

Ok, so this is just for my piece of mind in understanding this not directly related to the video series.

I understand that when you declare something public it means it can be accessed anywhere in the entire program, and that private declared objects are only accessible to the class its in, and can be retrieved through a public get method in the same class as the private.

But what I don't understand, is what the point is in making any sort of variable private? couldn't you just not call upon if you didn't need it, but leave it public in case you did want to use it, instead of the added extra work of a get method?

In other words I don't see the benefit in why you would want to keep in within a class specifically instead of just leaving it be and using it when you needed it. maybe some of you experts could shed some light on it for me, some real world examples would be helpful as would dumbing down the terms so a noob could get it.

Thank you!

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Dylan!

Great question! Thank you for asking, I'm sure you aren't the only one wondering this!

So, the most common reason you'd want to do this is to hide your implementation from users of your code. By only exposing publicly what you want users of your code to do, you are essentially allowing yourself to change the underlying implementation however you want. This will be more clear when we use objects and other data structures in the fields of the class. This practice of keeping your implementation details private is called encapsulation (I think this gives a pretty good overview of why).

Does that help clear things up?

Dylan Carter
Dylan Carter
4,780 Points

Yes thank you I understand a little more but im still not quite there.

So as far as the pez program is concerned, the mCharacterName is private because you don't want a user of the application to be able to change it..

But instead couldn't you just not include any part of the main code that would allow them to change in the first place? thats what im not understanding.

public static void main(String[] args) { // Your amazing code goes here... System.out.println("We are making a new Pez Dispenser."); PezDispenser dispenser = new PezDispenser("Yoda"); System.out.printf("The dispenser character is %s\n", dispenser.getCharacterName()); } }

it seems that using this mCharacterName, getCharacterName, and characterName all will have the same value depending on the input (yoda in this case), so why does it matter that the first one is private when the other 2 will be accessable with the same string attached?

I hope this question makes sense im not really even sure im asking in the correct way im pretty lost on the concept.

Craig Dennis
Craig Dennis
Treehouse Teacher

Ohhhh! I think I see the confusion. This PezDispenser object that you are creating might end up being used by a large team of many people, people you may not even know about. By marking fields as private and only providing a getter method, you are essentially telling these future users of your class that you do not want them to change the name of the PezDispenser after it has been created. You are communicating how you intend each instance to be used. That Main program is just one example of how it could be used, if you give this code out anyone could use it. We'll go over how to do this in future courses, but just know, the code you write is going to be so good, everyone is going to want to use it.

I'll restate... Imagine your code being used by others in the future. Does that help remove the mental blockage. For instance, assume that someone wanted to use your class to create a Web Version of your PezDispenser. The class, or blueprint, specifies explicitly how you intend it to be used.

That help?

Dylan Carter
Dylan Carter
4,780 Points

Yeah okay I get the point now. Much appreciated.

Now lets say I was doing something more independent with my code, like making an app for google play that I wanted to license only to me however that works...then would you need any of those private fields or is it specifically used for a program like you said used and edited by many people?

Hi Craig, I'm someone that uses analogies often in my life. In your videos leading up to the methods you used a radio to explain the difference between what the user interacts with and sees and what we as the coders would interact and see....so from what I gather the on/off button and the radio tuner would be public methods in the radio class, whereas the circuit board would be private? Along the lines of Dylan's question, I still don't understand why necessarily you'd want to hide parts of your code. Taking the analogy of the radio, are we trying to protect the radio user for electrocuting themselves or destroying the radio by working on the radio themselves? I feel like I still don't understand something fundamental about the difference(s) between and why we would use public vs. private class. Thanks for any insight you can provide. ~Abby

Craig Dennis
Craig Dennis
Treehouse Teacher

Hi abbymann ! Great question!

Electrocuting is a great addition to that analogy, that is definitely one reason. if you don't understand the underlying code, you might "hurt" yourself accidentally.

The main reason is because you really only want to expose methods that the user will be needing to call. Other people will be using your code and if you expose the methods and others use that code, you will have to support those methods forever. If you want to change the way your implementation works...for instance, switch it to a satellite radio implementation, you don't really have much options if you've let your implementation details out.

Another reason would be because we want our public methods to be clear and not polluted. Let's assume there is a changeArcs method (no idea what it does, I really don't know how radios work, that just sounds radio'y), we don't want that at the same level as power or change station right? There's no way to identify that to callers, and we want to present clear and usable APIs.

That make more sense?