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 Privacy and Methods

Diego Marrs
Diego Marrs
8,243 Points

Purpose of private? + Return Statements

Hello-

I have been thinking about this for a while: When should you use 'private' in your program?

Also, i'm stuck on return statements. What do return statements do and when should you use them?

Help is appreciated!

3 Answers

You want to use private when other classes will be using the respective class; this is where getter methods come in. The getter and setter methods prevents other classes from making unwanted changes to your instance variables and other data.

Return statement is what is returned from a method that doesn't have void in the method heading. public static void main doesn't return anything because of the word void in the heading; if I was to write:

public int findNumber()

The compiler would be looking for a return statement that would return the int that is specified in the heading since it doesn't have void there. You can return any kind of data type- you just need to specify that type in replacement of the "void" in your method heading.

I hope this makes sense to you.

De Juan Evans-Taylor
De Juan Evans-Taylor
337 Points

An easy way to look at it. when you get more advanced. Say your creating a video game and the objective is for the main character to slay a dragon and the max life of the character is 100, You wouldn't the user to be able to to keep raising his health so he wouldn't die, so you would set that object to private so he wouldnt have access to it.

Anas Rida
Anas Rida
8,183 Points

but how would a user have access to it?? you mean actually opening the file and altering the values???

Nice :)

If you have a class that has an instance variable set to private and you instantiate an object from that class you can only access it to the extent that the program will allow.

When a programmer is designing a class he knows how much access will be needed to execute the program; if he grants too much access to the person using the class for their program they could end up causing the program to behave in unusual ways so the private variable is given to limit how much access the user has.

At times you will find yourself importing libraries at the start of your program so you can access the classes and use them in your program; and you will see that the only way to use a portion of the class is to use a get() or set() method; this is because if you could access it in a public way you could easily change the values of the variables which would then cause that class to behave differently. One example would be an int count variable- if this variable was made public and it was only meant to be incremented by 1 then user could go in an say: count = 10; So now the program has been modified in an unexpected way. This is what encapsulation is all about- tying up any loose ends in your code.