File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Getters and setters advantages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getters and setters advantages?" Watch "Getters and setters advantages?" New topic
Author

Getters and setters advantages?

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
I have read many times that "make your instance variables private and provide getters and setters to access these private instance variables from outside the class."
I don't understand what is it's purpose?People say it will prevent misuse as instance variable cannot be modified directly.What i think is- So what, if not directly,but still we can modify those instance variables through setters.
So we can still misuse it in the same way, so where's the advantage?

Also have a look at one more doubt i have,
http://www.coderanch.com/t/597570/java/java/Difference-between-Implementation-Inheritance-interface
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Yes, instance variables should almost always be private. But we don't always provide getters and setters either. For classes that provide behavior (as opposed to just encapsulating state), the variables get modified and exposed indirectly by the methods that operate on them. If you're just blindly adding getters and setters for every member variable, you're doing it wrong.

Even in those cases where we do expose the members through get/set, there are several advantages to using methods rather than making the variables private.

  • We can change the implementation of the method, possibly even getting rid of the variable complete, and no client code has to change.
  • We can intercept calls that modify or query the value, such as to validate or transform the value being set, or to transform the value being retrieved.
  • We can provide caching, leaving a varaible unset until it's needed, then setting it when its getter is called.
  • We can provide runtime polymorphism. Subclasses can provdie different behavior for get/set, such as for testing or as part of an ORM framework. (Hibernate does this, I think.) This is not possible with direct access to public variables.
  • Introspection for JavaBeans looks for read/write methods for properties. By default these are named get/set, but can be configured to be somethign else.
  • We can add listeners for when properties change when there's a setter. We can't do that with direct access to public variables.
  • From an OO design perspective, even if the get/set methods just directly return/modify the variable, providing the method supports abstraction and data hiding, so that we don't need to know or care that that's what they're doing, and don't need to know or care if it changes.
  • Rohan Deshmkh
    Ranch Hand

    Joined: Aug 31, 2012
    Posts: 127
    OK thanks Jeff, i understood, so it is a good idea to make your instance variable private and provide getters and setters only to those instance variables that will be used by other classes.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38441
        
      23
    I am not sure you have understood it. You can provide getXXX methods for those instance fields which are used by other classes, and setXXX methods for fields which are set from other classes. Obviously you don’t always need both setXXX and getXXX methods for a particular field.
    Rohan Deshmkh
    Ranch Hand

    Joined: Aug 31, 2012
    Posts: 127
    Campbell Ritchie wrote:I am not sure you have understood it. You can provide getXXX methods for those instance fields which are used by other classes, and setXXX methods for fields which are set from other classes. Obviously you don’t always need both setXXX and getXXX methods for a particular field.

    Thanks i completely understood what you meant, thanks for putting it in a nice and understandable manner .
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38441
        
      23
    You’re welcome
    Remember you may not always want set methods for every field.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Getters and setters advantages?