Immutability in Java – What it takes to build an immutable class

Immutability means something that cannot be changed. In java an immutable class is one whose state cannot be changed once it has been created. This post aims to give a guideline on how to make a class immutable. The post contains an example of creating an immutable class.
The complete code is available on Github

The Java documentation gives a list of guidelines for creating an immutable class. We will try to understand it better.

To make a class immutable, follow the following steps:

  1. Declare the class as final.
  2. Make all its fields final and private.
  3. For all mutable fields, make sure that the class creates a copy and only returns the copy to the calling code.
  4. Do not provide any setter methods.

Let’s try to understand why we need to do all of the above for making a class immutable. We need to ensure that the subclasses do not override any of the class methods. Declaring the class as final ensures that the class cannot be overridden. A more sophisticated approach is to make the constructor private and use a factory method to create an instance of the class. Making all the fields private will ensure that the fields cannot be changed outside the class, and making them final will ensure that we do not alter the field even by mistake. For all the mutable fields, we are making sure that no other object from outside can change the data by creating a defensive copy of the object and only returning this copy to the calling code. Setter methods are usually used to change the state of the object and as the goal of an immutable class is to avoid state changes, hence we do not provide any setter methods.

Lets look at an example of Immutable class:

/**
* Created by anjana
*/
import java.util.Date;
/**
* Steps for making a class immutable:
* 1. Declare the class as final
* 2. Make all its fields final
* 3. For all mutable fields, class should make a defensive copy and only return the copy to the calling code
* 4. Do not provide any setter methods.
*/
public final class ImmutableClass {
/**
* Integer and String classes are immutable whereas Date class is mutable
*/
private final Integer immutableInteger;
private final String immutableString;
private final Date mutableDate;
public ImmutableClass(Integer i, String s, Date d) {
this.immutableInteger = i;
this.immutableString = s;
this.mutableDate = new Date(d.getTime());
}
public String getImmutableString() {
return immutableString;
}
public Integer getImmutableInteger() {
return immutableInteger;
}
public Date getMutableDate() {
return new Date(mutableDate.getTime());
}
@Override
public String toString() {
return immutableInteger + ", " + immutableString + ", " + mutableDate;
}
}
view raw ImmutableClass.java hosted with ❤ by GitHub

We have three fields in the class, first field of Integer type, second field of String type, and third field of Date type. The String and Integer classes are immutable, however the Date class is mutable. Thus, we create a new Date object when assigning the Date to the class field.
That’s all for immutability.

Reference: Java Documentation

One thought on “Immutability in Java – What it takes to build an immutable class

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s