Monday, 8 January 2018

Core Java | Minimize mutability or create an immutable class in Java

An immutable class is simply a class whose instances cannot be modified. All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object.

JDK immutable classes
Boxed primitive classes (wrapper classes)
BigInteger and BigDecimal etc.

How to make a class immutable?
1. Don’t provide any methods that modify the object’s state.
2. Ensure that the class can’t be extended.
3. Make all fields final.
4. Make all fields private.
This prevents clients from obtaining access to mutable objects referred to by fields and modifying these objects directly.
5. Make defensive copies.
Ensure exclusive access to any mutable components.

public List getList() {
     // defensive copy of the mutable field before returning it to the caller.
     return Collections.unmodifiableList(list);

If your class has any fields that refer to mutable objects, ensure that clients of the class cannot obtain references to these objects. Never initialize such a field to a client-provided object reference or return the object reference from an accessor.

import java.util.Date;
public final class ImmutableClass {

       public ImmutableClass(int id, String name, Date doj) {
     = id;
     = name;
              this.doj = new Date(doj.getTime());

       private final int id;
       private final String name;
       private final Date doj;

       public int getId() {
              return id;
       public String getName() {
              return name;

      * Date class is mutable so we need a little care here.
      * We should not return the reference of original instance variable.
      * Instead of a new Date object, with content copied to it, should be returned.
      * */
       public Date getDoj() {
              return new Date(doj.getTime()); // For mutable fields

import java.util.Date;
public class TestImmutable {
       public static void main(String[] args) {
              String name = "raj";
              int id = 1;
              Date doj = new Date();
              ImmutableClass class1 = new ImmutableClass(id, name, doj);
              ImmutableClass class2 = new ImmutableClass(id, name, doj);
      // every time will get a new reference for same object. Modification in              reference will not affect the immutability because it is temporary reference.
              Date date = class1.getDoj();

Ensure that the class can’t be extended?
public class Immutable {
     private final int value;

     public Immutable(int value) {
         this.value = value;

     public int getValue() {
         return value;

Now, suppose I do the following:

public class Mutable extends Immutable {
     private int realValue;

     public Mutable(int value) {

         realValue = value;

     public int getValue() {
         return realValue;
     public void setValue(int newValue) {
         realValue = newValue;

Why Immutability?
1. They are less prone to error and are more secure.

2. Immutable classes are easier to design, implement, and use than mutable classes.

3. Immutable objects are thread-safe so there are no synchronization issues.

4. Immutable objects are good Map keys and Set elements since these typically do not change once created.

5. Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged).

6. Immutability makes it easier to parallelize program as there are no conflicts among objects.

7. The internal state of the program will be consistent even if you have exceptions.

8. References to immutable objects can be cached as they are not going to change.(i.e in Hashing it provide fast operations).

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...