What is Immutable Class in Java

Upasana | December 08, 2019 | 2 min read | 1,409 views | Multithreading and Concurrency


When the state of object can not be changed after its construction then the object is called Immutable.

Benefits of Immutable Objects

Inherent thread-safety

Immutable objects are inherently thread-safe, thus help writing multi-threading code without much worries.

Cacheability

Because Immutable objects do not change their value, we can freely cache them and be confident that their value won’t change.

Good candidate for Hashing Keys

Immutable objects are good candidate for hash keys because their hashcode can be cached and reused for better performance. They make the best HashMap keys.

Easy to understand

Immutable classes are easy to understand, as they possess a single state, which is controlled by their constructor.

Which Objects should be made Immutable

Immutable classes are ideal for representing ADT’s (Abstract Data Type) value.

All classes should be designed to be immutable unless there is a specific reason not to do so.

— Joshua Bloch

Guidelines for Making a class Immutable

  1. All fields should be declared final

  2. Class itself is declared final so that the derived classes do not make it Mutable.

  3. this reference should not be allowed to escape during object construction such as in anonymous inner classes (for example adding action listener)

  4. Any field that contains reference to mutable objects (such as arrays, collections, StringBuffer, etc)

    • Are private

    • Are never returned or exposed to the caller

    • Are the only reference to the Objects that they refer

    • Do not change the state of the referenced object after the construction.

    • If mutable fields must be returned to the caller, then a defensive copy should be returned so that the changes do not reflect in the inner data structure.
      For example,

      Defensive copy of mutable object
      public List getList() {
           return Collections.unmodifiableList(list); (1)
      }
      1 defensive copy of the mutable field before returning it to caller
    • If a mutable Object is passed in the constructor (like an array), then Immutable class should first make a defensive copy of the mutable object before storing its reference.

You can refer to other post where we will see how to create immutable class that has mutable member variable


Multithreading and Concurrency:
  1. Java 8 Parallel Stream custom ThreadPool
  2. Java Concurrency Interview Questions
  3. ConcurrentModificationException in Java
  4. What is purpose of Collections.unmodifiableCollection
  5. Removing elements while iterating over a Java Collection
  6. ThreadLocal with examples in Java
  7. Design an Immutable class that has an java.util.Date member
See all articles in Multithreading and Concurrency
Top articles in this category:
  1. Design an Immutable class that has an java.util.Date member
  2. What is purpose of Collections.unmodifiableCollection
  3. What is Double Checked Locking Problem in Multi-Threading?
  4. What is AtomicInteger class and how it works internally
  5. Troubleshooting Deadlock in Java
  6. What is difference between sleep() and wait() method in Java?
  7. What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications

Recommended books for interview preparation:

Find more on this topic: