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.
package org.arpit.java2blog.bean;
import java.util.ArrayList;

public final class Country{
 
 private final String countryName; 
 private final ArrayList<String> listOfStates;
 
 public Country(String countryName,ArrayList<String> listOfStates) {
  super();
  this.countryName = countryName;
  this.listOfStates=listOfStates;
  
 }

 public String getCountryName() {
  return countryName;
 }

 public ArrayList<String> getListOfStates() {
  return  listOfStates;
 }
 
 public static void main(String args[])
 {
  ArrayList<String> listOfStates=new ArrayList<String>();
  listOfStates.add("Madhya Pradesh");
  listOfStates.add("Maharastra");
  listOfStates.add("Gujrat");
  
  Country country=new Country("India",listOfStates);
  System.out.println("Country : "+country.getCountryName());
  System.out.println("List of states : "+country.getListOfStates());
  // It will be added to the list because we did not use clone in getListOfStates
  country.getListOfStates().add("Kerala");
  // It will be added to the list because we did not use deep copy in constructor
  listOfStates.add("Rajasthan");
  System.out.println("Updated List of states : "+country.getListOfStates());
  
 }  
}
When you run the program, you will get below output:
Country : India
List of states : [Madhya Pradesh, Maharastra, Gujrat]
Updated List of states : [Madhya Pradesh, Maharastra, Gujrat, Kerala, Rajasthan]
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:
public ArrayList<String> getListOfStates() {
  return  (ArrayList<String>) listOfStates.clone();
 }
when you run the program after making above changes, you will get below output:
Country : India
List of states : [Madhya Pradesh, Maharastra, Gujrat]
Updated List of states : [Madhya Pradesh, Maharastra, Gujrat, Rajasthan]
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.
public Country(String countryName, ArrayList<String> listOfStates) {
  super();
  this.countryName = countryName;
  ArrayList<String> tempList = new ArrayList<String>();

  for (int i = 0; i < listOfStates.size(); i++) {
   tempList.add(listOfStates.get(i));
  }
  this.listOfStates = tempList;
 }
Lets check the final class which we have created after doing above changes.
package org.arpit.java2blog.bean;

import java.util.ArrayList;

public final class Country {
    // declared private final instance variable
 private final String countryName;
 // Mutable object
 private final ArrayList<String> listOfStates;

 public Country(String countryName, ArrayList<String> listOfStates) {
  super();
  this.countryName = countryName;
  // Creating deep copy for mutable object
  ArrayList<String> tempList = new ArrayList<String>();

  for (int i = 0; i < listOfStates.size(); i++) {
   tempList.add(listOfStates.get(i));
  }
  this.listOfStates = tempList;
 }

 public String getCountryName() {
     // Do not need to do cloning as it is immutable object
  return countryName;
 }

 public ArrayList<String> getListOfStates() {
     // Returning cloned object 
  return (ArrayList<String>) listOfStates.clone();
 }

 public static void main(String args[]) {
  ArrayList<String> listOfStates = new ArrayList<String>();
  listOfStates.add("Madhya Pradesh");
  listOfStates.add("Maharastra");
  listOfStates.add("Gujrat");
  String countryName="India";
  Country country = new Country(countryName, listOfStates);
  System.out.println("Country : " + country.getCountryName());
  // Lets try to change local variable countryName
  countryName="China";
  System.out.println("Updated Country : " + country.getCountryName());
  System.out.println("List of states : " + country.getListOfStates());
  // It will  not be added to the list because we are using clone in
  // getListOfStates
  country.getListOfStates().add("Kerala");
  // It will not be added to the list because we are using deep copy in
  // constructor
  listOfStates.add("Rajasthan");
  System.out.println("Updated List of states : " + country.getListOfStates());

 }

}
When you run above program, you will get below output:
Country : India
Updated Country : India
List of states : [Madhya Pradesh, Maharastra, Gujrat]
Updated List of states : [Madhya Pradesh, Maharastra, Gujrat]

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.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012