
Immutable class in java means that once we create an object of class we cannot change the content of that object. If someone is trying to change the contents a new object will be created.
Immutable objects are mainly useful in concurrent applications. They cannot be corrupted by thread intervention or seen in an inconsistent state because they cannot change state.
What does immutability mean? Immutability means unable to change or sensitive to change. It will be easier for you to do concurrent programming if you use immutable classes. Immutable classes ensure that the value is not changed in the middle, even if you don’t use synchronized blocks.
Table of Contents
Simple Steps to Create Immutable Class in Java
These are the simple Steps to Create custom Immutable class in Java. You have to follow the following steps to create an immutable class:
- Declare your class final, so that no other classes can extend it.
- Don’t provide setter methods for the fields/variables.
- Make all fields/variables final and private.
- Initialize all the fields via using a parameterized constructor.
This the sample form of custom immutable class where Example is the name of the class.
public final class Example {
// Variable with private and final modifiers.
private final int variable;
// Parameterized constructor
public Example(int variable) {
this.variable= variable;
}
public Example change(int variable) {
if (this.variable == variable) {
//Check the value of variable if it is equal to your provided value.
//If both are same then the content does not change, just return the current object.
return this;
} else {
//If the content changes, create a new object with the updated content.
return new Example(variable);
}
}
}
The class in which we will create main method
public class Main {
public static void main(String[] args) {
Example obj1 = new Example(10);
Example obj2 = obj1.change(10);
Example obj3 = obj1.change(20);
System.out.println(obj1 == obj2);
System.out.println(obj1 == obj3);
}
}
Output:
true
false
What are the advantages of Immutable Class?
There are following advantages of immutable classes or objects:
- Immutable classes are easy to create, test and use and such classes are automatically thread-safe and do not have synchronization issues.
- By using immutable class we don’t need copy constructor.
- The state of an immutable object will not change, so in the multiple places we can use the same object hence we can save a lot of memory.
What are the disadvantages of Immutable object?
Immutable objects are just like use and throw because we cannot reuse it. This type of object can generate a lot of garbage and potentially slow down the application so this is the primary disadvantage of immutable object.
Examples of the Immutable Class
The String class in Java is a good example of an immutable class. Other than String, the wrapper classes for the primitive types like Integer, Byte, Character, Short, Boolean, Long, Double, Float are the example of immutable class.
FAQ
Yes
Yes
No. String isn’t a wrapper class because it doesn’t wrap any parallel primitive types.