Skip to content
Related Articles

Related Articles

How to create Immutable class in Java?
  • Difficulty Level : Easy
  • Last Updated : 31 Oct, 2020
GeeksforGeeks - Summer Carnival Banner

Immutable class means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable. We can create our own immutable class as well. 

Following are the requirements: 

  • The class must be declared as final (So that child classes can’t be created)
  • Data members in the class must be declared as private (So that direct access is not allowed)
  • Data members in the class must be declared as final (So that we can’t change the value of it after object creation)
  • A parametrized constructor should initialize all the fields performing a deep copy (So that data members can’t be modified with object reference)
  • Deep Copy of objects should be performed in the getter methods (To return a copy rather than returning the actual object reference)
  • No setters (To not have the option to change the value of the instance variable)

Example to create Immutable class


import java.util.HashMap;
import java.util.Map;
// An immutable class
public final class Student {
    private final String name;
    private final int regNo;
    private final Map<String, String> metadata;
    public Student(String name, int regNo,
                   Map<String, String> metadata)
    { = name;
        this.regNo = regNo;
        Map<String, String> tempMap = new HashMap<>();
        for (Map.Entry<String, String> entry :
             metadata.entrySet()) {
            tempMap.put(entry.getKey(), entry.getValue());
        this.metadata = tempMap;
    public String getName() { return name; }
    public int getRegNo() { return regNo; }
    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;
// Driver class
class Test {
    public static void main(String[] args)
        Map<String, String> map = new HashMap<>();
        map.put("1", "first");
        map.put("2", "second");
        Student s = new Student("ABC", 101, map);
        // Uncommenting below line causes error
        // s.regNo = 102;
        map.put("3", "third");
        System.out.println(s.getMetadata()); // Remains unchanged due to deep copy in constructor
        s.getMetadata().put("4", "fourth");
        System.out.println(s.getMetadata()); // Remains unchanged due to deep copy in getter
{1=first, 2=second}
{1=first, 2=second}
{1=first, 2=second}

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.
This article is contributed by Abhishree Shetty. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :