How to create an Immutable class in Java - Example

An immutable class in Java is a class, whose object's states can not be changed once created like String, Integer, Float etc. In this article we will see what is an Immutable class, what are benefits of having an Immutable class and how to construct a perfect immutable class in Java.

Advantages of making a class Immutable

First lets discuss some of the basic benefits of having a Immutable class; this includes following points:

1) Immutable classes in Java and simple to create, test and run. These classes are by default thread safe because of no way to change the object states once created.

2) Immutable objects are best choice to be used as a key of HashMap because of non changeable states.

3) Because of fixed states, no undetermined or undesirable behavior when used in an application.

How to create an Immutable class in Java

To make a class fully immutable there are certain guidelines that should be followed:

1) Make all primitive variables(int, boolean), and immutable referenced variables(Integer, Float ..) private and final, so that their values can not be changed once created.

2) Make all referenced non-immutable variables(List, Date, Employee ...) private and final, additionally do not serve a direct reference to outer world, instead serve a copy of same object through getters.

3) Do not provide public setters to any type of variables to prevent any kind of unwanted behavior from outside the class.

4) Make the class "final" to prevent it's methods from overridden by any sub class.

Java Implementation of making a class Immutable

Lets apply above guidelines and create a immutable class having both primitive and referenced mutable and immutable variables.


import java.util.Date;

public final class ImmutableDemo {
	// primitive final can not be changed once assigned a value
	private final int premitive1;

	// immutable final can not be changed once assigned a value
	private final String immutable1;

	// immutable final can not be changed once assigned a value
	private final Integer immutable2;

	// final referenced variable can not point to any other object once created,
	// but value can be changed using reference so we are not going to serve the
	// same reference, instead we will serve a copy for of this
	private final Date mutable;

	public ImmutableDemo(int premitive1, String immutable1, Integer immutable2,
			Date mutable) {
		this.premitive1 = premitive1;
		this.immutable1 = immutable1;
		this.immutable2 = immutable2;
		this.mutable = mutable;

	public int getPremitive1() {
		return premitive1;

	public String getImmutable1() {
		return immutable1;

	public Integer getImmutable2() {
		return immutable2;

	// servving a copy of mutable Date object and not the actual reference
	public Date getMutable() {
		return new Date(mutable.getTime());

	public String toString() {
		return premitive1 + " " + immutable1 + " " + immutable2 + " " + mutable;
Now we are done with writing a fully immutable class in Java using guidelines, lets now test the class for immutability:


import java.util.Date;

public class Implementation {

	public static void main(String[] args) {
		ImmutableDemo im = new ImmutableDemo(1, "tbNext", 3, new Date());
		System.out.println("Immutable object before modification: " + im);

		// lets try to change states of immutable object
		midification(im.getPremitive1(), im.getImmutable1(),
				im.getImmutable2(), im.getMutable());

		System.out.println("Immutable object after modification: " + im);

	public static void midification(int premitive1, String immutable1,
			Integer immutable2, Date mutable) {
		premitive1 = 12;
		immutable1 = "burps";
		immutable2 = 65;
		mutable = new Date(12);

Output: Now lets see the output of out immutable class states, before and after modification try:

Immutable object before modification: 1 tbNext 3 Sat Aug 22 15:48:38 IST 2015
Immutable object after modification: 1 tbNext 3 Sat Aug 22 15:48:38 IST 2015

You can see the output above, we can say that we have implemented a perfect immutable class and even after trying to modify values, the actual object states are unchanged.

In this article we have seen, what is a immutable class in Java, why do we need immutable classes and how to create a perfect immutable class. In upcoming articles we will see more about Java and Other technologies.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.