immutable class in java

person shubham sharmafolder_openJAVAlocal_offeraccess_time November 17, 2024

In Java, an immutable class is a class whose instances (objects) cannot be modified once created. Any modification to an instance of an immutable class results in the creation of a new instance, leaving the original instance unchanged. String is the most common example of an immutable class in Java, but you can create your own immutable classes as well.

Why Use Immutable Classes?

Immutable classes offer several benefits, particularly in multi-threaded environments:

  1. Thread Safety: Immutable objects are inherently thread-safe because they cannot be modified after creation. Multiple threads can safely access immutable instances without requiring synchronization.
  2. Security: Immutable objects are less susceptible to accidental or malicious modification.
  3. Memory Efficiency: Because they do not change, they can be safely shared and reused, saving memory.
  4. Ease of Use: Their state consistency makes them easier to use and less error-prone.

How to Create an Immutable Class in Java

To create an immutable class, follow these guidelines:

  1. Declare the Class as final: This prevents other classes from extending it and modifying its behavior.

  2. Make All Fields final and private: This ensures that fields can only be assigned once (during construction) and cannot be accessed directly outside the class.

  3. Do Not Provide "Setter" Methods: Setters allow fields to be modified, which breaks immutability.

  4. Initialize All Fields in the Constructor: All fields should be initialized in the constructor, and once set, they should not be changed.

  5. Return Copies of Mutable Fields: If the class has fields that hold references to mutable objects (e.g., Date, List), return a copy of the mutable object rather than the object itself to prevent external modifications.

  6. Override equals() and hashCode(): Immutable classes should typically override these methods for correct behavior in collections.

Example: Creating an Immutable Class

Here’s an example of an immutable class representing a Person with a name and age:

Explanation of the Example

  • Immutable Fields: name, age, and birthDate are all private and final.
  • Defensive Copy: The birthDate field is of type Date, which is mutable. To prevent external changes to birthDate, we create a defensive copy in both the constructor and the getter.
  • No Setters: There are no setter methods, so once a Person object is created, its state cannot be changed.
  • equals() and hashCode() Implemented: These methods are overridden to ensure correct equality behavior, especially if instances are used in collections like HashSet.

Immutable Class with Collections

If your class contains a collection (e.g., List, Set), use defensive copying or an unmodifiable wrapper to maintain immutability.

In this example:

  • The hobbies field is initialized with an unmodifiable view of the list. This ensures the list cannot be changed externally.
  • Collections.unmodifiableList() wraps the list so that any attempt to modify it (add/remove elements) will throw an UnsupportedOperationException.

Summary Checklist for Creating Immutable Classes

  • Mark the class as final.
  • Make all fields final and private.
  • Initialize all fields in the constructor.
  • Do not provide setters.
  • For mutable fields, return defensive copies or unmodifiable views.
  • Override equals() and hashCode() if needed.

By following these steps, you can create immutable classes that are safe, thread-friendly, and efficient.

warningComments are closed.