aspose file tools*
The moose likes Java in General and the fly likes Getters and Setters Usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getters and Setters Usage" Watch "Getters and Setters Usage" New topic
Author

Getters and Setters Usage

Gagan Kinra
Greenhorn

Joined: May 14, 2012
Posts: 7
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 13, 2005
Posts: 39861
    
  28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

    Joined: Mar 17, 2011
    Posts: 8242
        
      23

    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


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39861
        
      28
    And you will find objects much easier to handle if you can make them immutable.
    Gagan Kinra
    Greenhorn

    Joined: May 14, 2012
    Posts: 7
    Thanks a lot everybody... especially Jeff Verdegan for wonderful explanation..
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    You're welcome!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Getters and Setters Usage