How to create immutable class in java

In this tutorial, we are going to see how to create immutable class in java.
Immutable class is class whose state can not be changed once created.
Example: String is best example for immutable class. Once you create a String, you can not change it.
Immutable class is very simple to understand, it has only one state. Immutable class is carefully instantiated by the constructor. Immutable classes are thread safe. This is biggest advantage of immutable class, you don’t need to apply synchronization for immutable objects. Immutable class can be useful while putting object of immutable class in HashMap or it can be used for caching purpose because its value won’t change.

Immutable objects are by default thread safe.

Steps for creating a immutable class:

  • Make your class final : 
    If you make your class final, no class will be able to extend it, hence will not be able override methods of this class.
  • Declare all instance variable with private and final : 
    If you make instance variable private, no outside class will be able to access instance variables and if you make them final, you can not change it.
  • Say no to setter methods :
    Don’t create setter method for any instance variables, hence there will be no explicit way to change state of instance variables.
  • Initialize all variables in constructor :
    You can initialize variables in constructor. You need to take special care while working with mutable object. You need to do deep copy in case of imutable objects.
  • Perform cloning of mutable objects while returning from getter method:
    If you return clone of object from getter method, it won’t return original object, so your original object will remain intact. I will explain this more in later part of this tutorial.

Read : Why String is immutable in java
Lets understand immutable class with a very simple example:
Lets create a simple class called Country.java.

When you run the program, you will get below output:

Above class is not immutable. There are two reasons for it :

  • We did not use clone in getListOfStates() method, so we are able to add “Kerala” to the listOfStates.
  • We did not do deep copy for listOfStates , so we are able to add “Rajasthan” to the list.
Lets use clone in getListOfStates() method and see the difference, just change getListOfStates() to below code:
when you run the program after making above changes, you will get below output:
If you notice, “Kerala” is not added to the list because we are returning clone of listOfStates in getListOfStates() method, so adding “Kerala” to country.getListOfStates() won’t affect original list.
We are one step closed to immutable class now. 
Lets change constructor to make deep copy of listOfStates object.
Lets check the final class which we have created after doing above changes.
When you run above program, you will get below output:
Country class is immutable class now. As you can see, we are doing deep copy for listOfStates, so “Rajasthan” will not be added to the listOfStates.

I hope it will help you to create immutable class in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *