How to Create Immutable Class in Java

In this tutorial, I am going to discuss what is immutable classes in java? How to create custom immutable class in java.

What is immutable class?

Immutable class means, once you created an object of a class you can’t change it’s state or attribute value. In Java, all the wrapper classes like Boolean, Short, Integer, Long, Float, Double, Byte, Char and String classes are the immutable class.

In Effective java, Joshua Bloch makes this compelling recommendation.


“Classes should be immutable unless there’s a very good reason to make them mutable….If a class cannot be made immutable, limit its mutability as much as possible.”

What’s the advantage of immutable objects?

i) Immutable classes are thread safe as state cannot be changed so no synchronization is required.

ii) Immutable objects are a good choice for keys for maps.

iii) An immutable class is good for caching purposes because you don’t have to worry about it’s state changes.

How to create immutable class with Mutable Object in java

We can make any class immutable by following these steps –

i) Make all fields/attributes private so that direct access is not allowed.

ii) Do not write any setter methods for a class. Setter methods are meant to change the state of an object which we don’t want in case of immutable class.

iii) A class must be final so that it cannot be extended. In this way, we can prevent subclasses to override methods.

iv) Add all arguments constructor for object creation.

v) Initialized all non-primitive mutable fields in a constructor by performing a deep copy.

vi) In getter methods, return the deep copy of non primitive or mutable classes rather than returning the actual reference.

How to Create Immutable Class in Java

To understand the above points, let’s create our own custom immutable class.

For this i am creating two classes Employee and Address. Address is the attribute of Employee class, it remain as it is. Let’s make Employee class as immutable class.

Employee class :

Employee class has one attribute address of type Address.

Address class :

i) No Setters – The first step is to remove setters for each attributes so that no one can change the object field value using setter methods .

After removing setters, the Employee class have only getters method.

ii) Add all arguments constructor – Add all arguments constructor so that object fields can be intialized during object creation.

iii) Protect class from being extended by making as final – Make class as final so that no other classes can extend and override the methods.

iv) Initialized all non-primitive mutable fields in a constructor by performing a deep copy.

By returning the deep copy ensures that no one change the mutable fields of the object.

v) In getter methods, return the deep copy of non primitive or mutable classes rather than returning the actual reference.

If we don’t perform deep copy than the actual reference of an object is returned which can be changed.

Programming video tutorials

Let’s run it and trying to modifying the attributes.

Output:

Programming questions on arrays

Programming questions on linked list

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.