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?
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.
Joined: Aug 31, 2012
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.
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.
Joined: Aug 31, 2012
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 .
Joined: Oct 13, 2005
Remember you may not always want set methods for every field.