I have been learning about object-orientated programming for the past few days. I am not sure why instance variables are declared private, so could someone explain the purpose? Thank you.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have been learning about object-orientated programming for the past few days. I am not sure why instance variables are declared private, so could someone explain the purpose? Thank you.
To prevent outside direct access. By forcing the use of setter/getter methods, you can make a variable read-only from outside of its class (you have a public getter but no public setter), you can completely hide the existence of variables from outside a class (no public setters or getters), or you can force a specific means of modifying a variable.
For example, if you had a variable which you wanted to make sure was always bounded between 5 and 50, you could write a setter like this:
This ensures the restrictions are ALWAYS guaranteed when an outside class attempts to modify the variable, and you don't have to check the restriction everywhere else in the code where you intend on using the variable.private int var; public setVar(int value) { if(value<5) { var = 5; } else if(value>50) { var = 50; } else { var = value; } }
Basically, there are a lot of advantages to forcing the use of setters/getters instead of allowing other classes free-roam with the variables. For more information, research Encapsulation: What is Encapsulation in Java and OOPS with Example
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
There is no direct benefit, but its more likely that you make less errors yourself (or others if they are using your code) if things are properly declared.
If we assume you were the perfect programmer who never makes mistakes you could make everything public.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Example:
public class Logger {
private String format;
public String getFormat() {
return this.format;
}
public void setFormat(String format) {
this.format = format;
}
}
there's no way for other classes to retrieve or set the format varable directly.
Nijan.G
Last edited by copeg; July 24th, 2014 at 08:52 AM. Reason: removed links
@Nijan: Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.
Please post your code correctly per the above link.
thanks a lot guys, this was helpful
When I hear this question I think of the Far Side comic where a passenger on a plane finds a switch on the armrest labelled "Wings stay on/Wings fall off"
Improving the world one idiot at a time!
Ada Lovelace (August 8th, 2014)