All TalkersCode Topics

Follow TalkersCode On Social Media

How To Create Immutable Class In Java Without Using Final

Last Updated : Jul 1, 2023

How To Create Immutable Class In Java Without Using Final

In this article we will show you the solution of how to create immutable class in java without using final, the state of immutable objects instances doesn't change after initialization.

For example, the value of the immutable class String never changes remains constant after it has been created. Let's see Why Strings in Java Are Immutable.

Step By Step Guide On How To Create Immutable Class In Java Without Using Final :-

The function Object() { [native code] } that provides deep copy should be commented, and the function Object() { [native code] } that provides shallow copy should be uncommented.

Additionally, in the getTestMap() method, which returns the real object reference, uncomment the return statement. After finishing all the changes, run the application. The result will be as follows.

As you can see from the result, shallow copy in the function Object() { [native code] } caused HashMap values to alter.

The getter function's direct reference to the original object is the cause of the issue.

For a Java immutable class, that is all. Additionally, we learnt how crucial deep copy is for immutable classes.

Further Reading: We can utilise the builder approach to create immutable classes if the immutable class contains a lot of attributes, some of which are optional.

There are so many immutable classes in Java like String, Boolean, Byte, Short, Integer, Long, Float, Double and more.

In brief, all the wrapper classes and String class is absolutely immutable. We can also create immutable class by creating final class that have some final data members.

In Java, an immutable class means that once an object is created by user, its content will never be changed.

All the following wrapper classes, such as Integer, Boolean, Byte, and Short, as well as String class, are totally immutable. We may even design our own immutable class.

Read through the immutability's qualities before moving on so that you may implement it with confidence.

Here are the prerequisites in order to prevent the creation of child classes, the class must be designated 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 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.

In the following example program, we will discuss about creating immutable class in Java without using final.

public class Sample{
   String name;
   int age;
   public Sample(){ = name;
      this.age = age;
   public String getName(){
   public int getAge(){
      return this.age;
  1. Creating a public class Sample
  2. Then we will create a string name and a variable of integer time name age.
  3. Then we will create a public sample method which will use this keyword to call name and age
  4. In the next step we will use integer int getAge method
  5. Then we will return this.age method
  6. Lastly we will run the code

Conclusion :-

So, in the above article we learned about creating immutable class without final variable. In the next tutorial we will discuss about java immutable object.

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