All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create Immutable Object In Java

Last Updated : Sep 1, 2023

How To Create Immutable Object In Java

In this article we will show you the solution of how to create immutable object in java, an immutable object in Java is one whose state can't be changed after creation. In other words, when an immutable object is created, its internal state doesn't change during the object's existence.

Immutable objects are useful in a variety of situations, including multithreading, caching, and preserving the integrity of data.

In this article, we'll look at a number of Java techniques for building immutable objects.

There are a few crucial actions we must take in order to generate an immutable object in Java.

First, we have to mark the class as final in order to forbid any subclassing that would alter the behaviour of the object.

The fields must then be designated as private and final so that they cannot be changed after being assigned.

Furthermore, we shouldn't give these fields any setter methods. Last but not least, we must make sure that any mutable reference fields in the class are appropriately enclosed and are not exposed for the outside world.

Step By Step Guide On How To Create Immutable Object In Java :-

package task;
public final class Immutable{
        private final int a;
        private final int b;
    public immutable(int a, int b) {
            this.a = a;
            this.b = b;
        }
        public int getA() {
            return a;
        }
        public int getB() {
            return b;
        }
    }
  1. Immutable is a public final class that is declared for the class. The final keyword denotes that the class cannot be subclassed, while the public keyword indicates that the class can be accessed from other classes.
  2. a and b, two private instance variables, are declared. These variables serve as a representation of the point's coordinates and are designated as final, meaning that once set they cannot be modified.
  3. The constructor public Immutable(int a, int b) is defined. The a and b parameters of this constructor are used to represent the coordinates of the point. The a and b parameters are allocated to the respective instance variables inside the constructor.
  4. The a coordinate value is returned by the getA() method, which is defined. A cannot have its value changed once it has been initialised because it is declared as final.
  5. The b-coordinate value is returned by the getB() method, which is defined. b is also designated as final and cannot be modified, much like getA().
  6. The Immutable class assures that once an object has been generated, its state cannot be modified by defining the a and b variables as final and without offering any methods to modify their values.

Conclusion :-

In Java, immutable objects must be created in order to preserve data integrity and prevent accidental changes.

This tutorial's instructions will help us design objects that won't alter after we make them. Immutable objects are very helpful in situations when data consistency and thread safety are crucial.

Keep in mind that immutability encompasses both blocking changes to an object's state as well as declaring its fields as final.

We may create code that is more reliable and easy to maintain by designing our classes having immutability in mind.

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