AppDividend
Latest Code Tutorials

How to Create Immutable Class in java Example Tutorial

0

Immutable Class In Java Example | Java Immutable Class Tutorial is today’s topic. Immutable classes are those who cannot be broken or whose content cannot be changed once an object is created. Immutable classes in java are like double, float, long, integer, short, byte, boolean, string, etc. To create an immutable class, we must create a final class as the content of the final class cannot be changed.

Immutable Class In Java

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

The immutable class is good for the caching purpose 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 the thread safety in the multi-threaded environment.

These are the steps to create an immutable class in Java

  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 that means data members needs to be of final type.
  3. Use a parameterized method.
  4. Don’t use the setter method, that means we don’t have the option to change any instance variable of the class.
  5. Use 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 in Java. 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 the new instance each time you try to change them.

#Example to create Immutable class

In this example, we have created the final class named Employee. It has one final 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 the value of it 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 the lot of advantages, especially when used correctly in the multi-threaded environment.

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

Finally, How to Create Immutable Class in java Example Tutorial is over.

Leave A Reply

Your email address will not be published.

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