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.

Results 1 to 7 of 7

Thread: What is the purpose for declaring things private.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the purpose for declaring things private.

    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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What is the purpose for declaring things private.

    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:
    private int var;
     
    public setVar(int value) {
    	if(value<5) {
    		var = 5;
    	}
    	else if(value>50) {
    		var = 50;
    	}
    	else {
    		var = value;
    	}
    }
    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.

    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/

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: What is the purpose for declaring things private.

    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.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the purpose for declaring things private.

    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

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is the purpose for declaring things private.

    @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.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the purpose for declaring things private.

    thanks a lot guys, this was helpful

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is the purpose for declaring things private.

    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!

  8. The Following User Says Thank You to Junky For This Useful Post:

    Ada Lovelace (August 8th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: April 8th, 2014, 12:05 PM
  2. What exactly is the purpose of a Scanner class?
    By ekrocks in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2014, 10:33 PM
  3. Replies: 5
    Last Post: March 20th, 2013, 02:29 PM
  4. Singleton pattern: the purpose of empty constructor
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: August 6th, 2010, 08:58 AM

Tags for this Thread