Immutable Java Class

Introduction

In this article we will learn about Immutable classes.
In Java, an immutable class is a class whose instance once created cannot be modified. Examples are below

  • String
  • LocalDate in Java 8
  • Wrapper Classes for Primitive Types like Integer, Long
  • Enum

To create a Java class immutable, we need to take care below points.

  • Make the class final , so that it cannot extend or inherit by another class.
  • Make all fields private and final.
    • Making field private, restrict its access within the class and cannot modify outside class.
    • If field is final, once initialize we cannot change its value, so it helps to restrict the modification of fields within the class.
    • Initialization of final fields only possible at the time of field declaration or using constructor.
  • Do not provide setter methods instead use a constructor to initialize all the fields.
  • Class can have getter methods, but Do not create any method that can modify state of class.
  • If class contains any reference of mutable object, then do not return actual object instead return its copy, so that mutable objects cannot modify outside immutable class. When return copy , always try to return deep copy not shallow copy.

Immutable class offer advantages in situations like

Concurrency control, Thread Safety in Multithreaded environment.

  • As state of an immutable object cannot be modified, so it can easily share and access among different thread without any need of explicit synchronization or concurrency control.
  • For Immutable objects in multithread environment,
    • No additional locking require using synchronize block or keyword.
    • No prevention against race condition, as race condition will not happen in case of immutable classes.
    • Data shared between multiple thread without any risk of data corrupt.
Caching
  • For caching immutable object, chances to provide invalid data is very less. As object state remain same, so in case any update happens then whole new object need to create and cache it again, instead of modification within existing object.
  • As state of an immutable object remain same throughout it lifetime, so no complex cache invalidation strategies require. Only just need to refresh cache with new object.
Instances to use as Key in HashMap.
  • Immutable classes is good choice to use as key in HashMap, because their state cannot be modified once their instance created.
  • But it is necessary for immutable classes also to override hashCode() and equals() method, otherwise default hashCode() method of Object class will generate different hashcode even if fields inside object has same value. Which make it not a good key for HashMap.
  • Example :
      ImmutableClass obj1 = new ImmutableClass(1, "abc", Arrays.asList(1,2,3,4,5));
      ImmutableClass obj2 = new ImmutableClass(1, "abc", Arrays.asList(1,2,3,4,5));
     If we don't override hashCode and equals method , hashcode of obj1 and obj2 are different.  But to use it as key, obj1 and obj2 must generate same hashcode.





Note : Please comment below for any improvement , suggestions, regarding any wrong information present in this article. We will improve it so that only right information available in this article.





Comments