How to Create Immutable Class in Java?
Last Updated :
30 May, 2025
In Java, immutability means that once an object is created, its internal state cannot be changed. Immutable classes in Java provide many advantages like thread safety, easy debugging and all. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and the String class is immutable. We can create our own immutable class as well.
In this article, we are going to learn:
- What immutability means
- Why it is useful
- How to create our own immutable class
- Why deep copying is important
- What are the limitations Java record types have
What is an Immutable Class?
An immutable class is a class whose objects cannot be changed once created. If we do any modification, it results in a new object. This method is used in concurrent applications.
Rules for Creating an Immutable Class
- The class must be declared as final so that child classes cannot be created.
- Data members in the class must be declared private so that direct access is not allowed.
- Data members in the class must be declared as final so that we can’t change their value after object creation.
- A parameterized constructor should initialize all the fields, performing a deep copy so that data members can't be modified with an object reference.
- Deep Copy of objects should be performed in the getter methods to return a copy rather than returning the actual object reference.
Note: There should be no setters or in simpler terms, there should be no option to change the value of the instance variable.
Example: Immutable class implementation
Student.java
Java
// Java Program to Create An Immutable Class
import java.util.HashMap;
import java.util.Map;
// declare the class as final
final class Student {
// make fields private and final
private final String name;
private final int regNo;
private final Map<String, String> metadata;
// initialize all fields via constructor
public Student(String name, int regNo, Map<String, String> metadata) {
this.name = name;
this.regNo = regNo;
// deep copy of mutable object (Map)
Map<String, String> tempMap = new HashMap<>();
for (Map.Entry<String, String> entry : metadata.entrySet()) {
tempMap.put(entry.getKey(), entry.getValue());
}
this.metadata = tempMap;
}
// only provide getters (no setters)
public String getName() {
return name;
}
public int getRegNo() {
return regNo;
}
// return deep copy to avoid exposing internal state
public Map<String, String> getMetadata() {
Map<String, String> tempMap = new HashMap<>();
for (Map.Entry<String, String> entry : this.metadata.entrySet()) {
tempMap.put(entry.getKey(), entry.getValue());
}
return tempMap;
}
}
In this example, we have created a final class named Student. It has three final data members, a parameterized constructor, and getter methods. Please note that there is no setter method here. Also, note that we don't need to perform deep copy or cloning of data members of wrapper types as they are already immutable.
Geeks.java:
Java
import java.util.HashMap;
import java.util.Map;
public class Geeks {
public static void main(String[] args) {
// create a map and adding data
Map<String, String> map = new HashMap<>();
map.put("1", "first");
map.put("2", "second");
// create an immutable Student object
Student s = new Student("GFG", 101, map);
// accessing data
System.out.println(s.getName());
System.out.println(s.getRegNo());
System.out.println(s.getMetadata());
// try to modify the original map
map.put("3", "third");
System.out.println(s.getMetadata());
// try to modify the map returned by getMetadata()
s.getMetadata().put("4", "fourth");
System.out.println(s.getMetadata());
}
}
Even after modifying the original or returned Map, the internal state of the Student object remains unchanged. This confirms the immutability concept.
Output:
GFG
101
{1=first, 2=second}
{1=first, 2=second}
{1=first, 2=second}
Limitation of Java record with Mutable Fields
Java 14 introduced record. This is a clear and concise way to define immutable like classes:
record Student(String name, int regNo, Map<String, String> metadata) {}
But this only offers shallow immutability. If the Map is modified externally, the internal state of the record changes:
Map<String, String> map = new HashMap<>();
map.put("1", "first");
Student s = new Student("ABC", 101, map);
// Changes internal state — NOT safe
map.put("2", "second");
s.metadata().put("3", "third");
Note: Use record only if all fields are immutable types like String, int, or other records.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read