How to create an immutable class in Java?
An immutable object is an object whose state cannot be modified after it is created.
Once we created an object we can't perform any changes in the existing object.
If we are trying to perform any changes with those changes a new object will be created.
If there is no change in the content then the existing object will be reused. This behavior is called immutability
In Java, all the wrapper classes Integer, Boolean, Byte, Short, and String class is immutable. We can create our own immutable class.
There are steps we need to follow while creating an immutable class.
1). Don't write code for setter methods, which means methods that modify fields or objects referred to by fields.
2). Make all fields final and private.
3). Declare the class as final If the other classes extend this, they prevented override methods.
4) Mae the class constructor private. Create an object of a class using a factory method.
5). Make all mutable fields final so that its values can be assigned only once.
If the instance fields include references to mutable objects, don't allow those objects to be changed:
Don't provide methods that modify the mutable objects.
Don't share references to the mutable objects.
Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies.
Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.
Here is an example to create immutable class
public final class Employee
{
final String name;
final String dept;
final int empNumber;
public Employee(String name, String dept,int empNumber)
{
this.name = name;
this.dept = dept;
this.empNumber = empNumber;
}
public String getName()
{
return name;
}
public String getDept()
{
return dept;
}
public int getEmpNumber()
{
return empNumber;
}
}
Test class
class EmployeeTest { public static void main(String args[]) { Employee emp1 = new Employee("Bala","Finance", 1000); Employee emp2 = new Employee("George", "Human Resource",1001); System.out.println(
emp1
.getName()+" "+
emp1.getDept()+" "+emp1.getEmpNumber());
System.out.println(emp2.getName()+" "+
emp2.getDept()+" "+emp2.getEmpNumber());
// emp1.dept = "finance"; //error
}
}
output:
Bala Finance 1000
George Human Resource
George 1001 1001
List of immutable classes in java
JDK itself has lots of immutable classes
1). Wrapper classes such as Integer, Long, Double, etc.
2). java.util.UUID
3). String
4). java.util.Locale
5) java.lang.StackTraceElement
Advantages of making a class immutable
1). We don't need to create a copy constructor.
2). We don't implementation of clone
3). Immutable classes are simple to construct, test, and use.
4). The immutable class automatically thread-safe and no need regarding synchronization issues.
5). You can make good Map keys and Set elements (these objects must not change state while in the collection.
We have seen What is immutable class and how to create an immutable java class. We also have seen the advantages of immutable classes while developing applications.,
0 Comments
Post a Comment