Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getters and Setters Usage

 
Gagan Kinra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have doubts in the usage of getters and setter. I know getters are used to get the value of instance variable and setters are to set the values.

but thru getters and setters we care exposing our private instance variable or even class variables to the client code, which itself breaks the security.

Class A{
private int a;
static int b;
public int c;
}

I am trying to understand why we need to provide setters and getters for private members ? and also do we provide setters and getters for class variable...if so reason behind it?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using setters you can prohibit invalid data assignation for your private instance variables thereby protecting your code from malfunctioning. And since you use the private variable, the clients shall get its value using the public getter method.

For ex:


This way you encapsulate your code -
Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Source - Object

I haven't used getters, setters for static variables - as static variables can be accessed using the class names and using instance methods to change its state is not a good coding practice.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be usual to throw an IllegalArgumentException in that instance. If you pass null (not possible for an int), you would probably want a NullPointerException thrown.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gagan Kinra wrote:
but thru getters and setters we care exposing our private instance variable or even class variables to the client code, which itself breaks the security.
...
I am trying to understand why we need to provide setters and getters for private members ? and also do we provide setters and getters for class variable...if so reason behind it?


You don't have to provide getters and setters for all your member variables. It's better to start off not doing it, and then only expose those that really have a reason to be exposed. I typically start by making my member variables private and final, and either initializing them at declaration (such as for collections) or directly from constructor parameters, or deriving them from constructor parameters and/or other member variables at construction.

Now, when you do decide that there's a reason to expose a particular member variable, there are several reasons to use get/set methods rather than just making it non-private:

  • You can validate the input, as already shown in this thread.
  • You can manipulate or transform the input, such as converting a String to an int, or putting a cap on a value, or rounding it.
  • You can return a calculated value, rather than storing it in a member.
  • You can defer setting a value until get() is called, possibly preventing unnecessary, expensive calculations.
  • You can provide a get() but no set(), making the variable publicly readable but not publicly writable.
  • You can use runtime polymorphism to enable a subclass to have different behavior, such as different validation rules or different calculations.
  • You can change the implementation without changing any client code. For example, add, remove, or change any of the above, without users of your class having to change their code or even be aware of your change.


  • You can't do any of the above if you just provide direct access, rather than using get()/set() methods.

    Again, though, if you just add plain old pass-through get()/set() to every member variable as a matter of course, you're doing it wrong.
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10417
    63
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:You don't have to provide getters and setters for all your member variables. It's better to start off not doing it, and then only expose those that really have a reason to be exposed.

    Amen.

    @Gagan: And, as you get further along, you'll discover that you often don't want to publicize them (particularly setters). It's usually much better to design your objects to act on messages based on business rules. You may find this article useful.

    Winston
     
    Campbell Ritchie
    Sheriff
    Posts: 48910
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And you will find objects much easier to handle if you can make them immutable.
     
    Gagan Kinra
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot everybody... especially Jeff Verdegan for wonderful explanation..
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic