{

Learn how to implement an immutable class in java with examples


This post covers an example for

  • Immutable objects in java
  • Creating immutable with java 14
  • How to create an immutable class with mutable references

What is an immutable class in java? An immutable class is a class once the class instance is created, Their content/state can not be changed.

Each object has instance member variables and methods which change the state. Immutable classes with instance members are not changed their state via methods. It’s changed the state of an object during object creation using the constructor.

Immutable is making class with design guidelines

Here are the rules to be followed to make a class immutable

  • Declare the class as final to prevent it from being extended by other classes.

  • All methods of the class should be final

  • setter is not allowed

  • instance member variable should always be private final

  • ANy method which doesn’t

  • All the instance variables are initialized using the constructor

How do you create an immutable class in java?

Here is a sequence of steps to make any class immutable

Here is the Immutable class definition in java

public final class User{

    private final int id;
    private final String name;
    public User(int id, String name)
    {
        this.id = id;
        this.name=name;
    }
    public int getId(){
        return id;
    }
    public String getName(){
        return name;
    }
    public static void main(String args[]){
        System.out.println("Hello");
    }
}

Advantages of immutable classes

  • Allows classes to be thread-safe for concurrent use of not allowing changing their state

  • This will be a useful caching purpose as the first time it creates an object with a default state and does not change state thereafter.

  • It allows not to extend or subclasses for inheritance

How to create an immutable class in the Java 14 version

with the java 14 version, You can easily create an immutable class

record is a keyword applied to java classes and it has no instance variables

public record User (id name, String name){
    this.id =id;
    this.name = name;
    public String getName() {
    return name;
  }
}

Record classes features

  • it is final, Can not be subclasses
  • It can have instance methods, static classes
  • No need to implement a constructor
  • equals, hashcode, and toString is not required to implement it

How to create an immutable class with a list of immutable objects

For suppose, the User object contains multiple roles.

Here User class is an immutable class The role is a mutable class

Here is an example for an immutable class referring to mutable object references

import java.util.List;
import java.util.ArrayList;

final class User
{
    final int id;
    final String name;
    final List<Role> roles;
    Immutable(int id,String name,List<Role> roles)
    {
        this.id=id;
        this.name=name;
        this.roles=roles;
    }

    public List<Role> getRoles()
    {
        List<Role> roles=new ArrayList<>(this.roles);
        return roles;
    }

}
class Role implements Cloneable
{
    String name;
    Role(String name)
    {
        this.name=name;
    }
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.