Wednesday, February 13, 2013

Immutable object in java


Immutable objects are the  one whose state can't be changed once they are constructed. Example objects of class String and Integer. Immutable objects are considered useful in concurrent applications because threads can't change their state.






How to create an immutable object ?

1. Ensure that the class can't be overridden . The simplest way is to make the class final.   Another way is to make the construstor private and use factory methods to construct instances.


2. Make all the fields private and final.


3. Do not provide any methods that can change object's state . For example  don't provide any setter methods to change object's state.


4. If the instance members refer to any mutable objects, don't allow those objects to get changed. This can be done in following ways :



  • Don't provide methods that can 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.
  • Never return the original mutable object in the methods, if necessary create copies of mutable objects and return them.

Example 
To demonstrate this behavior , we can take example of class java.lang.String


String oldString = new String("Old String");

System.out.println("Before : " + oldString);
oldString.replace("Old", "new");
System.out.println("After  : " + oldString);

The output will be:

Before : Old String
After     : Old String

Here we can see that the contents of the string oldString did not change. It is because the objects of class String are immutable.


6 comments :

  1. So the immutable objects can only be used in multi-threading applications ? What is the motivation behind using them in normal programs?

    String being an immutable, will thus create a new object. every time we want to change its behavior.

    ReplyDelete
    Replies
    1. Hi Aakash,

      There are several advantages of using immutable objects:

      1) Immutable objects are automatically thread-safe, the overhead caused due to use of synchronization is avoided.

      2) Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state.

      3) The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction.

      4) The best use of the immutable objects is as the keys of a map.

      Delete
  2. What i see in your example is that oldString.replace("old", "new"); doesn't do anything. This is because the replace() method is case sensitive and in oldString you have "Old", not "old".

    ReplyDelete
    Replies
    1. ioio , thats a mistake . The statement should be oldString.replace("Old", "new") , with capital O and not small .

      But the point is , replace method will return a new string object instead of changing the existing one.

      Delete
  3. Ioio more to the point replace returns a new string object and doesn't change the receiving string object

    ReplyDelete