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.”