What is the advantage of immutability and Rules and Example


Immutable Class

Immutable objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.


Example without mutable refs

public final class CustomColor {
    final private int customRed;
    final private int customGreen;
    final private int customBlue;
 
    private void validate(int customRed, int customGreen, int customBlue) {
        if (customRed < 0 || customRed > 255 || customGreen < 0 || customGreen > 255 || customBlue < 0 || customBlue > 255) {
            throw new IllegalArgumentException("Invalid color values");
        }
    }
 
    public CustomColor(int customRed, int customGreen, int customBlue) {
        validate(customRed, customGreen, customBlue);
        this.customRed = customRed;
        this.customGreen = customGreen;
        this.customBlue = customBlue;
    }
 
    public CustomColor invert() {
        return new CustomColor(255 - customRed, 255 - customGreen, 255 - customBlue);
    }
}

What is the advantage of immutability?

The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.

By having an immutable object, one can ensure that all threads that are looking at the object will be seeing the same state, as the state of an immutable object will not change


What is the advantage of immutability?

The following rules define a simple strategy for creating immutable objects.

  • Don't provide "setter" methods - methods that modify fields or objects referred to by fields.
  • Make all fields final and private.
  • Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  • If the instance fields include references to mutable objects, don't allow those objects to be changed:
  • Don't provide methods that modify the mutable objects.
  • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

Example with mutable refs

In this case class Point is mutable and some user can modify state of object of this class

class CustomPoint {
    private int customX, customY;
 
    public CustomPoint(int customX, int customY) {
        this.customX = customX;
        this.customY = customY;
    }
 
    public int getCustomX() {
        return customX;
    }
 
    public void setCustomX(int customX) {
        this.customX = customX;
    }
 
    public int getCustomY() {
        return customY;
    }
 
    public void setCustomY(int customY) {
        this.customY = customY;
    }
}
 
public final class CustomImmutableCircle {
    private final CustomPoint customCenter;
    private final double customRadius;
 
    public CustomImmutableCircle(CustomPoint customCenter, double customRadius) {
        // Create a new object here to ensure immutability
        this.customCenter = new CustomPoint(customCenter.getCustomX(), customCenter.getCustomY());
        this.customRadius = customRadius;
    }
}

Basic Programs