Learn how to implement immutable class in java with examples


This post covers an example for

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

What are immutable class in java? Immutable class is a class once class instance is create, Their content/state can not be changed.

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

Immutable is making class with design guidelines

Here are the rules to be follow to make class as immutable

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

  • All methods of class should be final

  • setter is not allowed

  • instance member variable should be private final

  • ANy method which don’t

  • All the instance variables are initialized using constructor

How do you create a immutable class in java?

Here are sequence of steps to make any class as immutable

Here is 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 its state

  • This will be useful caching purpose as first time it creates and object with default state and not changing state thereafter

  • It allows not to extend or sub classes for inheritance

How to create a immutable class in java 14 version

with java 14 version, You can easily create a immutable class

record is an 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 constructor
  • equals, hashcode and toString is not required to implement it

How to create immutable class with list of immutable objects.

For suppose, User object contain multiple roles.

Here User class is immutable class Role is an mutable class

Here is an example for immutable class referring 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.