Immutable class means unmodifiable or unchangeable, i.e., once the object has been created, they are immutable objects, there is no way to modify the text it represents. In Java, all primitive java.lang package wrapper classes (like String, Boolean, Byte, Short, Integer, Long, Float, Double, etc.) and String classes are immutable.
In object-oriented and functional programming, immutable objects (unchangeable objects) are like constants. They are final and are used for caching purpose. It marks class members as private. However, it can be quite useful for eliminating the unintentional changes in the data of the object. Thus, each object has its copy of the instance variables. This is necessary as each object needs to hold a different set of values for its instance variables. So modifying the instance variables of one object does not change the instance variables of any other objects of the same class.
Whenever the objects of a class are instantiated, each object will have its copy of instance variable(s). However, all objects share only one copy of each instance method of the class.
We’ll be covering the following topics in this tutorial:
Advantages of using Immutable class
• Thread safe: Immutable objects are always thread-safe, then it contains no race conditions.
• Key in HashMap: Immutable classes like primitive wrapper classes are can be used as key in HashMap.
• HashCode is cached: HashCode of Immutable classes is a unique code (not to calculate hashcode again) caching by the JVM at time of object creation. Hence, This will dramatically improve application performance.
• Parallel programming: Immutability makes it easier to parallel programming by eliminating the need of locking, and consequently.
• Consistent state: An immutable object will be consistent state even if you have exceptions.
Example of create Immutable class in Java:
public final class Registration {
final Integer regNo;
final String name;
final String password;
public Registration(Integer regNo, String name, String password) {
this.regNo = regNo;
this.name = name;
this.password = password;
}
public Integer getregNo() {
return regNo;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
}
The above class is immutable because:
• Instance variables are class variables. It must be declared as final. The final(non-access modifier) is a keyword in java, so it can’t be subclassed. It’s a unique copy of the field in each object (an instance of a class).
• A final class is a class that can’t create the subclass. Also, methods could be declared as final to indicate that cannot be overridden by subclasses.
• Don’t provide setter methods, So You can’t modifying the instance variables once they are created.
Some popular immutable classes
In java, A String objects is the most popular immutable class in java, once initialized its contents cannot be modified. Does the below code change the contents of the string?
String name = “Dinesh”;
name = “Thakur”;
The answer is no. We will be focusing on the first line being a positional string object with the content “Dinesh” and assigns its reference to name . The string on newline into with the content “Thakur” and assigns its reference to name . At this point, there are still first objects that exist after the assignment, but it can no longer be accessed, because variable name creates a new integer object, as shown in Figure below.