What is an Immutable class and How to create one ?

Photo by Spenser H on Unsplash

What is an Immutable class and How to create one ?

What is an immutable class?

Immutable class are the kind of classes who doesn't allow to change the data of their objects once they are created.

In normal language, if you create an object of a immutable class and set the data of that object while creating the object then after that you won't be able to update the data of the object.

How to create an immutable class ?

In order to create an immutable object we have to make sure of few things.

  1. we have to set the data of the object during the object creation process and that can be done through parameterized constructor.

  2. we must avoid setter methods so that no one will be able to set new data to the object.

  3. we have to restrict the inheritance so that no one can extend the class and create setter methods in child class to update the data of the object.

  4. we have to return a copy of the data in getter methods (if getter method returns an object of non-immutable class) rather than the original data so that no one can update that data.

Lets implement this :

import java.util.Date;
public final class ImmutableClass {
    private final String data1;
    private final Date data2;
    private final int data3;

    // Constructor to initialize the object
    public ImmutableClass(String data1, Date data2, int data3) {
        this.data1 = data1;
        // Create a defensive copy of the Date object
        this.data2 = new Date(data2.getTime());
        this.data3 = data3;
    }

    // Getter methods for accessing the data
    public String getData1() {
        return data1;
    }

    public Date getData2() {
        // Return a defensive copy of the Date object
        return new Date(data2.getTime());
    }

    public int getData3() {
        return data3;
    }
}

Lets understand the above class:

  • we have made the class final so that no one can create a child class.

  • we have a parameterized constructor to set the values while creating the object.

  • In Constructor we are not assigning the date object directly, we are creating another date object with same value and assigning that, this is called defensive copy.

    why defensive copy ? because someone can create an Date object and store its reference in a reference-variable and pass that object using that reference-variable in the constructor as a parameter and later on he can change that Date object using the reference-variable. To avoid that rather then using the object which came from parameter we can create a new object with same data and assign that.

  • we are not using any setter method.

  • for the getter method of Date object, we are not retuning the original date object because we don't want anyone to have original object and update it later. To achieve this we are returning a new object with same data.

Hope you understood the concept.

Did you find this article valuable?

Support Hemant Besra by becoming a sponsor. Any amount is appreciated!