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 3 of 3

Thread: Why do we make variables of a class private?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why do we make variables of a class private?

    I know that variables are made private in simple pojos so as to implement the concept of encapsulation. But my question is why do we do that if we indirectly access the same object's variable through getters and setters to either get the value or modify it. We can even directly access by the variable without making it private which also accounts to lesser code.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why do we make variables of a class private?

    There are a few different reasons:

    1. It abstracts the design. For example, say you have a rectangle class. How are you going to represent the rectangle? With width/height and the location of the top-left corner? Or by coordinates of the top-left and bottom-right corners? Or the bottom-left and top-right corners? With getter/setters, these functions don't have to directly return the underlying implementation, you can have multiple methods and ways for the user to use your class indepedent of the underlying implementation.

    2. You can add checks before a class enters a "bad" state. For example, what if the user tries to set the width of a rectangle to -1? If you give direct access to fields, there's no way to prevent this. However, with a setter you can implement advanced logic and handle situations accordingly.

    3. Along with reason 1, what if you decide at a later date you don't like the underlying implementation? If external code has direct access to variables then these change will require them to change every part of their code which uses your class. However, if we provide getter/setters, as long as the contract is the same, you only have to change the getter/setter implementations and everyone can continue to use your code exactly as they did before.

    4. You can provide "internally modifiable only" variables by only providing a public getter. For example, take the ArrayList class. It has an internal size field which should only be modified internally. However, it's also very useful for external code to retrieve the size of an ArrayList.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Pratyush (February 1st, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why do we make variables of a class private?

    Thanks for very clear and transparent answer. I seem to get it now more and more. Basically what it means is that we follow such standards so as to enhance flexibility and maintainability of the code not only for the current situation but also for future requirements.

Similar Threads

  1. private static variables
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: June 28th, 2012, 01:00 AM
  2. [SOLVED] paintComponent is not reading my private variables
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 25th, 2010, 02:31 PM
  3. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM

Tags for this Thread