An immutable object is an object whose state cannot be modified after its creation. Once object is created, you can not change the properties of it. Immutable object is sort of design pattern. We have to follow certain rules or patterns to create immutable objects.
In java, once you initialized an object , you’ll never be allowed to set the values of its property through the setter methods and not to be allowed to have any method that will change the state of the object. You will only be allowed to set the values at the time of object creation through constructor arguments. Mutable property of the class will never be assigned even through the constructor. Instead, it will copy the values from the arguments and getter method will also make another copy to return to the caller.
One last thing is that it’s a final class, You will never be allowed to inherit it, which means you can not create sub class of it.
We can get a better understanding, when we look at the below code.
Example:
public final class Product {
private final String productId;
private final String productName;
private final String companyName;
private final List versions;
public List getVersions() {
return new ArrayList(versions);
}
public String getProductId() {
return productId;
}
public String getProductName() {
return productName;
}
public String getCompanyName() {
return companyName;
}
public Product(String productId,String productName,String companyName,List versions) {
this.productId = productId;
this.productName = productName;
this.companyName = companyName;
this.versions = new ArrayList(versions);
}
}
String and Integer objects are the examples of immutable objects in JDK.
Summarising the guidelines below,
Final class – can not create subclass
No public setter method – Can not set or change properties
No public method that will change the object state
Constructor argument- set properties, while creating objects.
Copy mutable property – while setting through constructor and returning through the getter.
When/where to use:
We are fetching some meta data from the data base and those data should not be updated by the user. So here, we can store those data in an immutable object and use it to avoid altering the original data.
Let’s consider Amazon or Flipkart application. You can not modify the price, item name, description, review comments,etc in the item which you want to buy. So you can have immutable objects in your code to hold all these metadata to avoid modifying and updating the values in database.
In a multithreaded environment, we can safely use the immutable objects. If we use mutable objects without any need in such places, possibly there could be the chances of creating concurrency issues. For example, one thread is trying to change the state of an object, while another thread is accessing the state of it.
To avoid this, we have to make methods synchronized. Synchronizing methods may cause performance issues. So if we use immutable objects in multithreaded environment, we can come out of these issues and focus only on the other part of implementations.
Caching immutable objects is safer than the mutable objects. If a map has objects for its keys, we have to consider using immutable objects to avoid changing states. Because in most cases, hashcode value is calculated based on the object state.
For Javascripts, visit http://flexydevcenter.blogspot.com/