Question

Possible Duplicate:
Why use getters and setters?

I know this is very trivial. But why do we define private and then we use getter and setters. Is this more like for preventing the programmers from making mistakes on using private variables or methods? Or is it for security reasons. If for security reasons then what is the point of having getters or setters? I know that we can have restrictions inside getter and setter but those if clauses are mostly for preventing the mistakes not the privacy restrictions. E.g. we don't usually say for these members limit the access to this method with an if clause.

Thanks.

Was it helpful?

Solution

The practice of automatically, unthinkingly creating getX() and setX() methods for every variable is bad. It's mostly pointless, as you have observed.

But it's the unthinking part that's bad. Methods are infinitely better than exposed variables because, again, as you observed, you can control how the underlying data is accessed. If you think about what you're doing, and apply mutators and accessors as appropriate, they're a wonderful tool:

  • You can restrict clients to only reading, but not writing to a variable.
  • You can restrict the values of an integer to only positive values (for example).
  • You can ensure that one variable is always equal to one-half the value of another (for example).
  • You can ensure that, any time the value of a variable is changed, an event is sent to notify other clients.
  • You can change the data type of an underlying variable, perform conversions in the public methods, and not break any clients.
  • You can convert an object to a remote client for a server-side version of the same object, and again, the client code won't change.
  • You can ensure strict memory ordering across threads by using synchronized accessors.

These are only some of the things you can do with mutators and accessors. By using them, you make your software easy to change and easy to maintain.

OTHER TIPS

If one allows variables from a class to be edited by anyone, then Very Bad Things can happen. (for instance, if an unwary user changes a denominator to zero, we could destroy the universe [or get a divide by zero error]). Defining get() and set() methods allow you (the programmer) to define exactly how others can interact with your variables.

We use private fields and have getters and setters so those private fields can be accessed by other classes. If I have two classes in a project, say Car.java and Driver.java, we want Driver objects to be able to access the private field "wheel" in Car.java (or else the Driver cannot drive the car). But if the field is private, it can only be accessed with a getter method that returns the value of the field. Setters allow outside classes to alter the values of fields. You can avoid using getters and setters by making your fields public (or protected, where appropriate) instead of private.

it allows you to control access to things..... by encapsulating access to a piece of data, you are free to change where that data lives, or even you may make it computable. ie, rather than returning a value of a field, you do some calculation.

But, as good as getters and setters are, if you use them too much, then its a sign you aren't getting a object to do the work you need it to.

There are one more benefit, if you use the getters and setters, it allows a subclass to override how to access the information with a method and more flexibility.

With the help of an example let me explain the point. Suppose we have the following class:

class User {
    private String username;
    private String password;
}

Now in a team project where multiple programmers are working. Programmer A has developed the User class and programmer B has to work on the GUI interface. The policy decided is that the password field cannot be less than 5 characters. But unless you have not made the data fields private both A and B have to always check the validity of the new password being assigned to the password field.

Now suppose after making them private you implement the following setter method:

public boolean setPassword( String temp ) {
    if( temp.length() >= 5 ) {
        this.password = temp;
        return true;
    }
    else {
        return false;
    }
}

This way only A has to code it once and B has to just use it without worrying about the policy. This always encourages the DRY (Don't repeat yourself) rule.

Setter amd Getter Method basically used to restrict to make any changes by the other person. In Java language we are providing Data Hiding concept. This is achieved due to get and set method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top