Getters and setters enforce the concept of encapsulation in object-oriented programming.
By having the states of the object hidden from the outside world, the object is truly in charge of itself, and cannot be altered in ways that aren't intended. The only ways the object can be manipulated are through exposed public methods, such as getters and setters.
There are a few advantages for having getters and setters:
1. Allowing future changes without modification to code that uses the modified class.
One of the big advantage of using a getter and setter is that once the public methods are defined and there comes a time when the underlying implementation needs to be changed (e.g. finding a bug that needs to be fixed, using a different algorithm for improving performance, etc.), by having the getters and setters be the only way to manipulate the object, it will allow existing code to not break, and work as expected even after the change.
For example, let's say there's a setValue
method which sets the value
private variable in an object:
public void setValue(int value)
{
this.value = value;
}
But then, there was a new requirement which needed to keep track of the number of times value
was changed. With the setter in place, the change is fairly trivial:
public void setValue(int value)
{
this.value = value;
count++;
}
If the value
field were public, there is no easy way to come back later and add a counter that keeps track of the number of times the value was changed. Therefore, having getters and setters are one way to "future-proof" the class for changes which may come later.
2. Enforcing the means by which the object can be manipulated.
Another way getters and setters come in handy is to enforce the ways the object can be manipulated, therefore, the object is in control of its own state. With public variables of an object exposed, it can easily be corrupted.
For example, an ImmutableArray
object contains an int
array called myArray
. If the array were a public field, it just won't be immutable:
ImmutableArray a = new ImmutableArray();
int[] b = a.myArray;
b[0] = 10; // Oops, the ImmutableArray a's contents have been changed.
To implement a truly immutable array, a getter for the array (getArray
method) should be written so it returns a copy of its array:
public int[] getArray()
{
return myArray.clone();
}
And even if the following occurs:
ImmutableArray a = new ImmutableArray();
int[] b = a.getArray();
b[0] = 10; // No problem, only the copy of the array is affected.
The ImmutableArray
is indeed immutable. Exposing the variables of an object will allow it to be manipulated in ways which aren't intended, but only exposing certain ways (getters and setters), the object can be manipulated in intended ways.
I suppose having getters and setters would be more important for classes which are part of an API that is going to be used by others, as it allows keeping the API intact and unchanged while allowing changes in the underlying implementation.
With all the advantages of getters and setters said, if the getter is merely returning the value of the private variable and the setter is merely accepting a value and assigning it to a private variable, it seems the getters and setter are just extraneous and really a waste. If the class is going to be just for internal use by an application that is not going to be used by others, using getters and setters extensively may not be as important as when writing a public API.