How to Create Immutable Class in Java

Immutable objects are instances whose state doesn’t modify after being initialized. For instance, the String is an immutable class, and once instantiated, its value never changes.

Immutable Class In Java

An immutable class inJavaa is a class that cannot be broken or whose content cannot be changed once an object is created. Immutable classes in Java are double, float, long, integer, short, byte, boolean,Stringg, etc. To create an immutable class, we must create a final class as the content of the last class cannot be changed.

The immutable class is good for caching because you don’t need to worry about the value changes.

Another benefit of the immutable class is that it is inherently thread-safe, so you don’t need to worry about thread safety in the multi-threaded environment.

These are the steps to create an immutable class inJavaa

  1. The final keyword must be used before the class name so that we cannot make its child class.
  2. When the class is of the final type, data members need to be of the final type.
  3. Use a parameterized method. Please don’t use the setter method, which means we don’t have the option to change any class instance variable.
  4. Use the get method for the variable in it like getName().

See the following code example.

final class car // immutable class
{
  final String car_name;

  public car(String car_name) {
    this.car_name = car_name;
  }

  public String getName() // get method
  {
    return car_name;
  }
}

class immutable {
  public static void main(String args[]) {
    car c = new car("Mercedes-benz");
    System.out.println(c.getName());
  }
}

See the following output.

How to Create Immutable Class in java Example

Some Popular Immutable Classes

The String is the most famous immutable class inJavaa. Once initialized, its value cannot be modified.

We can perform the operations like trim(), substring(), replace(), always return the new instance, and don’t affect a current instance.

Another example from the JDK is the wrapper classes like Integer, Float, Boolean… these classes don’t change their state; however, they create a new instance each time you try to change them.

Example of creating Immutable class

In this example, we have created the final class named Employee. It has one last data member, a parameterized constructor and getter method.

See the following code.

public final class Worker {
  final String aadharNumber;

  public Employee(String aadharNumber) {
    this.aadharNumber = aadharNumber;
  }

  public String getAadharNumber() {
    return aadharNumber;
  }
}

The above class is immutable because:

  1. The instance variable of a class is final, i.e., we cannot change its value after creating the object.
  2. A class is final, so we cannot create a subclass.
  3. There are no setter methods, i.e., we have no option to change the value of the instance variable.

Conclusion

Immutable classes provide a lot of advantages, especially when used correctly in the multi-threaded environment.

The only disadvantage of the Immutable class is that they consume more memory than a traditional class since upon each modification of them, a new object is created in memory… but, the developer should not overestimate the memory consumption as it’s negligible compared to the advantages provided by these type of classes.

That’s it for this article.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.