How We Can Create our Own Immutable Class in Java – Definition, Benefits | Examples to Create Java Immutable Classes

In this tutorial, we will learn how to create an immutable class in java. First, before creating our own immutable class, we have to learn or remember what is immutability. For a better understanding of the concept, we have explained all the required details about how to create an immutable class in Java.

What is an immutable class in Java?

Immutable objects are occurrences whose state doesn’t change after it has been initialized. Immutable class means once we create an object we are not allowed to change the content of that object. If there is no change in the content existing object will be reused.

In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class are immutable. We can create our own immutable class as well.

Benefits of Immutable Class in Java

  • An immutable class is good for caching purposes as you don’t have to worry about the value changes.
  • It is inherently thread-safe, hence you don’t have to worry about thread safety in the case of a multi-threaded environment.

Now let’s discuss how to create an immutable class in Java.

How to Create an Immutable Class in Java?

The following rules define a simple strategy for creating an immutable class:

  1. Declare the class as final so it can’t be extended.
  2. Make all the fields final and private. So that direct access is not allowed.
  3. Don’t provide “setter” methods for variables. These methods modify fields or objects referred to by fields.
  4. Make all mutable fields final so that their value can be assigned only once.
  5. Use the getter method for all the variables in it.
  6. Initialize all the fields via a parameterized constructor that performs the deep copy.
  7. Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

How to Create Immutable Class in Java

Rules to Create Immutable Classes

There are certain guidelines to be followed while creating an Immutable Class in Java. We will help you figure out what these guidelines actually mean by telling you how to create an immutable class in Java with a mutable object. They are as follows

  • Don’t Provide any Setter Methods as they modify fields or objects referred to by fields.
  • Declare all fields final and private
  • Don’t encourage subclasses to override methods
  • Pay Special Attention while dealing with mutable instance variables.

Immutable Classes in JDK

Apart from the classes written by you, JDK itself has defined a lot of immutable classes. They are outlined below for your reference.

  • String
  • Wrapper classes such as Integer, Long, Double etc.
  • java.lang.StackTraceElement
  • Java enums (ideally they should be)
  • Immutable collection classes such as Collections.singletonMap() etc.
  • java.util.UUID
  • java.util.Locale

Do Read:

Example to create Immutable class

Example Program to Create Immutable Class in Java

public final class Test{
//private and final data member.
private final int i;
//parameterized constructor.
Test(int i){
this.i = i;
}

/*modify method with return type test.
because of this modify method test
class becomes immutable. */
public Test modify(int i){
/*check current objet i value is equal to your provided value(passing argument in constructor).
if both are same no change in the content therefore we return current object only. */
if(this.i == i){
return this;
}
/*if there is change in the content, create a new object with updated i value and return that object. */
else{
return new Test(i);
}
}
public static void main(String args[]){
Test t1 = new Test(10);
Test t2 = t1.modify(100);
Test t3 = t1.modify(10);

System.out.println(t1 == t2); //false
System.out.println(t1 == t3); //true;
}
}