Immutable class is the one whose state cannot be changed after it is created. The best example for this is String,Integer class in Java, once instantiated the value never be changed. In this article, lets learn How to Create an Immutable Class in Java.
Immutable class has lots of advantages such as they can be used for the caching purposes as you don’t need to worry about the value changes. Immutable class is thread-safe and hence you don’t need to worry about synchronization issues and can be easily used in the multi-threaded environment.
How to Create an Immutable Class in Java?
You can make the class immutable easily by following the below pointers
*Don’t provide “setter” methods or methods that modify fields or objects referred to by fields.
As the setter method will allow you to change the state of the object and hence avoid it.
* Make all fields final
and private
.
Making the fields private will let you access the fields only within the class and final keyword will let you prevent the value being changed at any cost. This increases your immutability feature.
* Prevent overriding
The best way to prevent overriding is declaring your class as a final class.
* Factory method instance creation and Private Constructor
Use factory based instance creation, have a public method to get the object instances. Private Constructor will never allow object creation for your class
* Never pass the reference of the Mutable objects
Immutable object like String,Integer can be passed directly whereas never pass the direct reference of the mutable object, instead create a copy and pass it
Lets take, Date class in java which is mutable even though you mark it with final keyword
final Date date = new Date(); date.setYear(2014); // This lets the value to be changed.
So in order to make it immutable we will return the copy not the direct reference of the mutable object
Date(date1.getTime());
Here date1 is a mutable object and its reference is not passed directly.
Let put all these and create the immutable class.
import java.util.Date; public final class ImmutableClassExample { //Both String and Integer is Immutable private final String val1; private final Integer val2; //Date is a Mutable field private final Date date1; public ImmutableClassExample(String val1,Integer val2,Date date1) { this.val1=val1; this.val2=val2; this.date1=new Date(date1.getTime()); } public String getVal1() { return val1; } public Integer getVal2() { return val2; } public Date getDate() { return new Date(date1.getTime()); } public static ImmutableClassExample getImmutableClassExampleObject(String a,Integer b,Date c) { return new ImmutableClassExample(a,b,c); } public String toString() { return val1+" --- "+val2+" --- "+date1; } }
Testing our immutable class
import java.util.Date; public class ImmutableTestMain { public static void main(String[] args) { ImmutableClassExample ic =ImmutableClassExample.getImmutableClassExampleObject("Java",1,new Date()); System.out.println(ic); ImmutableTestMain.changeValues(ic.getVal1(),ic.getVal2(),ic.getDate()); System.out.println(ic); } public static void changeValues(String val1,Integer val2,Date d) { val1="interview"; val2=100; d.setDate(10); } }
You will get the output as
Java --- 1 --- Thu Apr 10 15:43:25 IST 2014 Java --- 1 --- Thu Apr 10 15:43:25 IST 2014
Hence we are sure that the class which we have created is a immutable class. 🙂 Let me know if you have any different thoughts !!
Nissi says
Simply Super Bro!!! Thank you very much.
Tim says
why you made public constructor?
javainterviewpoint says
A public constructor is needed in order to initialize the variables