Inmutability

Immutability refers to the property or characteristic of an object, value, or state that cannot change once it has been created or established. In the context of programming, immutability implies that once an object is created, no modifications can be made to its internal state.

In OOP and Java, we can create immutable objects that are characterized by having values that do not change after their creation, which means that direct modification operations cannot be performed on them. Instead, to make changes, new objects based on the existing values are created.

Immutability offers several advantages, such as ease of reasoning about code behavior, the ability to share objects without worrying about unexpected modifications, and performance improvements since synchronization is not required in concurrent environments.

To achieve immutability in Java, there are some guidelines to follow:

  1. Declare the class as final: By declaring a class as final, it prevents other classes from inheriting it and modifying its behavior or state.
  2. Declare fields as final: Marking the fields of the class as final ensures that they cannot be modified after initialization in the constructor.
  3. Do not provide mutator methods (setters): Immutable objects should not have methods that allow changing their internal state. Instead, provide accessor methods (getters) to read the values of the fields.
  4. Avoid shared mutable state: If an immutable object contains references to other objects, those objects should also be immutable or managed in a way that they cannot be modified from outside the immutable object.

Let’s see how Immutability is applied in Java.

public final class ImmutableClass {
private final int value;
private final String name;
private final List<String> items;
public ImmutableClass(int value, String name, List<String> items) {
this.value = value;
this.name = name;
// Create a new instance of the list to avoid external modifications
this.items = new ArrayList<>(items);
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
public List<String> getItems() {
// return a copy of our current list to avoid to modify the original one
return new ArrayList<>(items);
}
}

In this example, we can see the guidelines applied to create a Java Immutable class.

Declare the class as final

The ImmutableClass is declared with the final modifier to prevent inheritance with the extends.

Declare fields as final

The attribute’s value, name, and items are declared as final, which means that once assigned, their values cannot be changed.

For the items attribute, a new instance of ArrayList is created based on the passed list argument. This ensures that no modifications can be made to the original list from outside the class.

Do not provide mutator methods (setters)

We need to provide the class only with the accessor methods (getters) for the attributes, which means there are no mutators methods (setters) to modify the attribute values.

Avoid shared mutable state

In the constructor, the initial values are assigned to the attributes value, name, and items. For the items attribute, a new instance of ArrayList is created based on the passed list argument. This ensures that no modifications can be made to the original list from outside the class.

Additionally, for the items attribute, we need to do the same as in the constructor, create a copy of the list and return it in the getItems() method to prevent modification of the original list from outside the class.

After explaining using the guidelines how to apply the immutability in the ImmutableClass, we can say that “when we need to create objects of this class, the attribute values are set once in the constructor and cannot be modified afterward. This ensures that objects of the class are immutable, meaning their state does not change after being created”.

Final Notes!!! Immutability is important in various domains, such as functional programming and concurrent programming.

  • In Functional programming: There is an emphasis on creating immutable objects to avoid side effects and ensure referential transparency.
  • In Concurrent programming: Immutable objects are used to prevent concurrency issues, as multiple threads can access them without the need for synchronization.

In summary, immutability in Java refers to the inability of an object to change after its creation, which provides benefits in terms of security, concurrency, and code clarity.

happy Learning!!!

Leave a comment