How do you write an immutable class in JAVA?

One of the preferred questions of the interviewers is

What is an immutable object? Can you write an immutable class?

In Java, an immutable object is the object which once created it cannot be modified. Respectively, an immutable class is a class whose objects cannot be modified once created. The most common example of immutable class in Java is the String class (which btw, is marked as final to prevent subclassing. This choose was made in order to restrict subclassing from altering the immutability of the parent class). To achieve the immutable functionality of a class you declare it’s members as private and modify them only in the constructor of the class.

A strategy to defining immutable objects could be defined as follow:

  1. do not provide setter methods in your class (only getters are allowed);
  2. mark all fields as private and final;
  3. do not allow subclasses to override the methods in your class. One possible way of achieving this behavior is to mark your class as final;
  4. do not share references to the mutable object. Create copies of them.

An example of immutable class

Let see first, the Address object which will use as field in our immutable object:

The immutable class will be defined as:

Why want to use immutable object

Let’s extend this topic purpose and talk about the advantages of the immutable objects vs. mutable objects. They provide concurrency and multithreading advantages over the mutable objects. For example, immutable object can be shared between multiple threads without providing an external synchronization. In other words, immutable objects are by default thread-safe.

Another advantage is that immutable classes can be used as keys in Map or Set. This is because they eliminates the possibility of data becoming unreachable.

Key take away:

  • Any modification to an immutable object will result in the creation of a new immutable object.
  • Objects of java.lang.String class are the most know examples of immutable objects. Any change on existing string object will result in another string.
Spread the love

Leave a Reply