All TalkersCode Topics

Follow TalkersCode On Social Media

How To Make A Class Immutable In Java

Last Updated : Jan 1, 2023

How To Make A Class Immutable In Java

In this article we will show you the solution of how to make a class immutable in java, in Java, an immutable class means that once an object is created, its content can never be changed.

All wrapper classes in Java language, such as Integer, Boolean, Byte, and Short, as well as the String class, are all totally immutable.

We may even design our very own immutable class. We must Read through the immutability's qualities before moving on so that you can implement it with confidence. Here are the preconditions:

Step By Step Guide On How To Make A Class Immutable In Java :-

  • In order to prevent the creation of child classes, the class must always be declared as final.
  • To prevent direct access, data members of the class must be made private.
  • To prevent value changes after object creation, data members in classes must be marked as final.
  • To prevent value changes after object creation, data members in classes must be marked as final.
  • To prevent data members from being changed by an object reference, a parameterized function Object() { [native code] } should do a deep copy initialization of all the fields.
  • Instead of returning the real object reference, getter methods should execute a deep copy of the object to return a copy.

When an object of an immutable class is created in Java, its value cannot be altered. String is an example of an immutable class. As a result, once a string is produced, its content cannot be changed.

In addition, we can design our own unique immutable classes. What must be done in order to construct an immutable class is as follows.

Put the class in final mode to prevent extensions.

There should be no setter methods to modify the values of class members; instead, all class members should be private so they cannot be accessible outside of the class.

The copy of class members should be returned by the getter method.

only the function Object() { [native code] } is used to initialise class members.

Immutable classes prevent field modifications or changes once the object of the class is formed. The immutable classes in Java include the Boolean, Short, Integer, Long, Float, Double, Byte, Char, and String classes.

The benefits of immutable classes or objects include the following:

The state of an object's fields cannot be altered or changed.

Without considering whether a method alters or modifies any of the object fields, it can be supplied to any method.

The hash code of the object is simple to cache. These items make excellent keys for maps. Because the object's state won't change, these objects work well in environments with several threads.

Since the state of an immutable object won't change, it can be utilised in various places, conserving memory.

public final class Employee
final String pancardNumber;
public Employee(String pancardNumber)
public String getPancardNumber(){
return pancardNumber;
public class ImmutableDemo
public static void main(String ar[])
Employee e = new Employee("ABC123");
String s1 = e.getPancardNumber();
System.out.println("Pancard Number: " + s1);
  1. First step creating a final class Employee.
  2. We will create a final string pancardNumber;
  3. Thirdly, we will create a method public Employee (String pancardNumber)
  4. Then using ‘This’ keyword we will call pancardNumber
  5. Then we will create another method String getPancardNumber() that will get the value and then return the value.
  6. Then we will create class ImmutableDemo
  7. We will create main method
  8. After that we will create object of the class Employee using new operator and a default constructor will be called. Employee e = new Employee("ABC123");
  9. Then we will create String S1 and call the getPancardNumber method.
  10. Lastly will print a statement Pancard Number: " + s1, which will be stored in string s1.
  11. Run the code

Conclusion :-

So, in this segment we learnt about making immutable class in Java. In next tutorial we will learn on reversing strings in java.

I hope this article on how to make a class immutable in java helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials