Overriding Getters Setters vs. Public Fields

Why do we keep instance variables private? We don’t want other classes to depend on them. Moreover it gives the flexibility to change a variable’s type or implementation on a whim or an impulse. Why, then programmers automatically add or override Getters and Setters to their objects, exposing their private variables as if they were public?

Accessor methods

Accessors (also known as Getters and Setters) are methods that let you read and write the value of an instance variable of an object.

public class AccessorExample {
    private String attribute;

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}

Why Accessors?

There are actually many good reasons to consider using accessors rather than directly exposing fields of a class

Getter and Setter make API more stable. Consider a field public in a class which is accessed by other classes. Later on, we want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. Any changes to this public field will require changes to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data or lazily initialization etc.

Accessor method also allows us to fire a property changed event if the new value is different from the previous value.

Another advantage of using setters to set values is that we can use the method to preserve an invariant or perform some special processing when setting values.

All this will be seamless to the class that gets a value using the accessor method.

Should I have Accessor Methods for all my fields?

Fields can be declared public for package-private or private nested class. Exposing fields in these classes produces less visual clutter compare to accessor-method approach, both in the class definition and in the client code that uses it.

If a class is package-private or is a private nested class, there is nothing inherently wrong with exposing its data fields—assuming they do an adequate job of describing the abstraction provided by the class.

Such code is restricted to the package where the class is declared, while the client code is tied to class internal representation. We can change it without modifying any code outside that package. Moreover, in the case of a private nested class, the scope of the change is further restricted to the enclosing class.

Another example of a design that uses public fields is JavaSpace entry objects. Ken Arnold described the process they went through to decide to make those fields public instead of private with gets and sets methods here

Now this sometimes makes people uncomfortable because they've been told not to have public fields; that public fields are bad. And often, people interpret those things religiously. But we're not a very religious bunch. Rules have reasons. And the reason for the private data rule doesn't apply in this particular case. It is a rare exception to the rule. I also tell people not to put public fields in their objects, but exceptions exist. This is an exception to the rule because it is simpler and safer to just say it is a field. We sat back and asked: Why is the rule thus? Does it apply? In this case, it doesn't.

Private fields + Public accessors == encapsulation

Consider the example below

public class A {
    public int a;
}

Generally, this is considered bad coding practice as it violates encapsulation. The alternate approach is

public class A {
   private int a;

   public void setA(int a) {
      this.a =a;
   }

   public int getA() {
      return this.a;
   }
}

It is argued that this will encapsulate the attribute. Now is this really an encapsulation?

The Fact is, Getters and Setters have nothing to do with encapsulation. Here the data isn't more hidden or encapsulated than it was in a public field. Other objects still have intimate knowledge of the internals of the class. Changes made to the class might ripple out and enforce changes in the dependent classes. Getter and setter in this way are generally breaking encapsulation. A truly well-encapsulated class has no Setters and preferably no Getters either. Rather than asking a class for some data and then compute something with it, the class should be responsible for computing something with its data and then return the result.

Consider an example below,

public class Screens {
    private Map screens = new HashMap();

    public Map getScreens() {
        return screens;
    }

    public void setScreens(Map screens) {
        this.screens = screens;
    }
    // remaining code here
}

If we need to get a particular screen, we do code like below,

Screen s = (Screen)screens.get(screenId);

There are things worth noticing here....

The client needs to get an Object from the Map and casting it to the right type. Moreover, the worst is that any client of the Map has the power to clear it which may not be the case we usually want.

Alternative implementation of the same logic is:

public class Screens {
    private Map screens = new HashMap();

    public Screen getById(String id) {
        return (Screen) screens.get(id);
    }
// remaining code here
}

Here the Map instance and the interface at the boundary (Map) are hidden.

Getters and Setters are highly Overused

Creating private fields and then using the IDE to automatically generate Getters and Setters for all these fields is almost as bad as using public fields.

One reason for the overuse is that in an IDE it’s just now a matter of few clicks to create these accessors. The completely meaningless Getters and Setters code is at times longer than the real logic in a class and you will read these functions many times even if you don't want to.

All fields should be kept private, but with Setters only when they make sense which makes object Immutable. Adding an unnecessary Getters reveals internal structure, which is an opportunity for increased coupling. To avoid this, every time before adding the Accessors, we should analyse if we can encapsulate the behaviour instead.

Let’s take another example,

public class Money {
    private double amount;

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    //client
    Money pocketMoney = new Money();
    pocketMoney.setAmount(15d);
    double amount = pocketMoney.getAmount();  // we know its double
    pocketMoney.setAmount(amount + 10d);
}

With the above logic, later on, if we assume that double is not a right type to use and should use BigDecimal instead, then the existing client that uses this class also breaks.

Let’s restructure the above example,

public class Money {
private BigDecimal amount;

    public Money(String amount) {
    this.amount = new BigDecimal(amount);
}

    public void add(Money toAdd) {
        amount = amount.add(toAdd.amount);
    }

    // client
    Money balance1 = new Money("10.0");
    Money balance2 = new Money("6.0");
    balance1.add(balance2);

}

Now instead of asking for a value, the class has the responsibility to increase its own value. With this approach, the change request for any other datatype in future requires no change in the client code. Here not only the data is encapsulated but also the data which is stored or even the fact that it exists at all.

Conclusions

Use of Accessors to restrict direct access to field variable is preferred over the use of public fields, however, making Getters and Setters for each and every field is overkill. It also depends on the situation, though, sometimes you just want a dumb data object. Accessors should be added to the field where they're really required. A class should expose larger behaviour which happens to use its state, rather than a repository of state to be manipulated by other classes.

More Reading

https://c2.com/cgi/wiki?TellDontAsk

https://c2.com/cgi/wiki?AccessorsAreEvil

Effective Java