Write a Java Program to Create an Immutable Class

In Java, an immutable class is a class whose objects cannot be modified once they are created.

This means that their state cannot be changed after instantiation.

In this tutorial, we will learn how to create an immutable class in Java.


Declare the Class as Final

To make the class immutable, we need to declare it as final.

This prevents the class from being subclassed and its behavior changed.

public final class ImmutableClass {
  // class implementation goes here
}

Declare All Instance Variables as Final

All instance variables of the immutable class should be declared as final.

This ensures that their values cannot be changed once they are initialized.

public final class ImmutableClass {
  private final int variable1;
  private final String variable2;
  
  public ImmutableClass(int variable1, String variable2) {
    this.variable1 = variable1;
    this.variable2 = variable2;
  }
}

Do Not Provide Setter Methods

To make the class immutable, we should not provide any setter methods that can modify its state.

public final class ImmutableClass {
  private final int variable1;
  private final String variable2;
  
  public ImmutableClass(int variable1, String variable2) {
    this.variable1 = variable1;
    this.variable2 = variable2;
  }
  
  // do not provide any setter methods
}

Declare All Methods as Final

All methods of the immutable class should be declared as final.

This ensures that the behavior of the class cannot be changed by any subclass.

public final class ImmutableClass {
  private final int variable1;
  private final String variable2;
  
  public ImmutableClass(int variable1, String variable2) {
    this.variable1 = variable1;
    this.variable2 = variable2;
  }
  
  // do not provide any setter methods
  
  public final int getVariable1() {
    return variable1;
  }
  
  public final String getVariable2() {
    return variable2;
  }
  
  // declare all methods as final
}

Return Cloned Object in Constructor and Getter Methods

To prevent the original objects from being modified, we should return a cloned object in the constructor and getter methods.

public final class ImmutableClass implements Cloneable {
  private final int variable1;
  private final String variable2;
  
  public ImmutableClass(int variable1, String variable2) {
    this.variable1 = variable1;
    this.variable2 = variable2.clone();
  }
  
  // do not provide any setter methods
  
  public final int getVariable1() {
    return variable1;
  }
  
  public final String getVariable2() {
    return variable2.clone();
  }
  
  // declare all methods as final
  
  @Override
  public ImmutableClass clone() {
    return new ImmutableClass(this.variable1, this.variable2);
  }
}

Override the equals() and hashCode() Methods

To ensure that the objects of the immutable class can be used as keys in hash-based collections, we need to override the equals() and hashCode() methods.

public final class ImmutableClass implements Cloneable {
  private final int variable1;
  private final String variable2;
  
  public ImmutableClass(int variable1, String variable2) {
    this.variable1 = variable1;
    this.variable2 = variable2.clone();
  }
  
  // do not provide any setter methods
  
  public final int getVariable1() {
    return variable1;
  }
  
  public final String getVariable2() {
    return variable2.clone();
  }
// declare all methods as final

@Override public ImmutableClass clone() { return new ImmutableClass(this.variable1, this.variable2); }

@Override public boolean equals(Object obj) { if (obj == this) { return true; }
   if (!(obj instanceof ImmutableClass)) 
{
  return false;
}

ImmutableClass other = (ImmutableClass) obj;

return this.variable1 == other.variable1 && this.variable2.equals(other.variable2);
}

@Override public int hashCode() { int result = 17; result = 31 * result + variable1; result = 31 * result + variable2.hashCode(); return result; } }                                          

Conclusion

In this tutorial,we learned how to create an immutable class in java.