• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getters and setters advantages?

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 50168
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 50168
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome
    Remember you may not always want set methods for every field.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic