Some of the readers may not be aware of what is immutable and for that purpose I have tried to add some basic information about Immutability.

What is Immutability:

If you have a class for which you don’t allow the modification of the objects once they have been created, then it is known as immutable class and the objects are known as immutable objects. The modification here we are talking about is the modification of member variables (also known as state of object) for the object. You can create Immutable objects in any OOPS language like Java and C#.Net.

How to create Immutable classes:

There are few guidelines to create immutable objects like

  • Mark class as final
  • Private member variables
  • No public setter methods
  • No public getter methods which return the member reference variables
  • Modification methods returning new objects with modified contents instead of actually modifying the original object

Some examples of immutable classes in JDK: String, Wrapper classes like Interger, Float, Short etc.

Weak and Strong Immutability:

The last point (5) in the above list of points talks about creating a copy of original object and then modifying its contents before returning it. If you are aware of Shallow and Deep Cloning then you must be aware of subtle issues with Shallow cloning where the member reference variables are shared among the original and copy object. This results in modification of member reference object whether it is modified by using the parent original or parent copy object.

Similar to Shallow cloning , if we don’t create a completely new copy of original object then the reference variables could be shared among the two objects which we think are going to be immutable.
As with cloning, we have different terminology for the two cases where we have completely independent immutable objects and objects with shared reference member objects. The terms are Strong Immutability and Weak Immutability.

Weak immutability

The following code example demonstrates who weak immutability is implemented:

  • This program can be run as it is to see the output
  • Spare me for not adding javadoc and code comments
  • Ideally Member and Immutable classes should be in different files. I added them as single file only to make sure that it is easy to run the program and see output.
package com.example;

class Member{
    private int a;

    Member(int a){
        this.a = a;
    }

    public int getA(){
        return a;
    }

    public void setA(int a) {
        this.a=a;
    }
}

class Immutable {

    private Member m;

    Immutable(Immutable t){
        this.m = t.m;
    }

    Immutable(int a){
        m = new Member(a);
    }

    public int getMember(){
        return m.getA();
    }

    private void setMember(int a){
        this.m.setA(a);
    }

    public Immutable modifyM(int a) {
        Immutable t1 = new Immutable(this);
        t1.setMember(a);
        return t1;
    }
}

public class Test{

    public static void main(String[] args) {
        Immutable im1  = new Immutable(10);
        System.out.println("Before Modify, im1 member is " + im1.getMember());
        Immutable im2 = im1.modifyM(20);
        System.out.println("After Modify, im1 member is " + im1.getMember());
        System.out.println("After Modify, im2 member is " + im2.getMember());
    }
}

Output:

Before Modify, im1 member is 10
After Modify, im1 member is 20
After Modify, im2 member is 20

Strong immutability

The following code example demonstrates how strong immutability is implemented:

Notice the difference in the statements in yellow background.

package com.example;

class Member{
    private int a;

    Member(int a){
        this.a = a;
    }

    public int getA(){
        return a;
    }

    public void setA(int a) {
        this.a=a;
    }
}

class Immutable {

    private Member m;

    Immutable(Immutable t){
        this.m = new Member(t.getMember());
        //this.m = t.m;
    }

    Immutable(int a){
        m = new Member(a);
    }

    public int getMember(){
        return m.getA();
    }

    private void setMember(int a){
        this.m.setA(a);
    }

    public Immutable modifyM(int a) {
        Immutable t1 = new Immutable(this);
        t1.setMember(a);
        return t1;
    }
}

public class Test{

    public static void main(String[] args) {
        Immutable im1  = new Immutable(10);
        System.out.println("im1 member is " + im1.getMember());
        Immutable im2 = im1.modifyM(20);
        System.out.println("im1 member is " + im1.getMember());
        System.out.println("im2 member is " + im2.getMember());
    }
}

Output:

im1 member is 10
im1 member is 10
im2 member is 20

Related Posts

Implementing Weak and Strong Immutability in Java admin Core Java
Some of the readers may not be aware of what is immutable and for that purpose I have tried to add some basic information about Immutability. What is Immutability: If you have a class for which you don’t allow the modification of the objects once they have been created, then it...
Some of the readers may not be aware of what is immutable and for that purpose I have tried to add some basic information about Immutability. <h2>What is Immutability:</h2> If you have a class for which you don’t allow the modification of the objects once they have been created, then it is known as immutable class and the objects are known as immutable objects. The modification here we are talking about is the modification of <a href="http://www.javaexperience.com/eclipse-get-access-modifier-suggestions-using-ucdetector-plugin/" title="Get access modifier suggestions using Eclipse ucdetector plugin">member variables</a> (also known as state of object) for the object. You can create Immutable objects in any OOPS language like Java and C#.Net. <h3>How to create Immutable classes:</h3> There are few guidelines to create immutable objects like <ul> <li>Mark class as final</li> <li>Private member variables</li> <li>No public setter methods</li> <li>No public getter methods which return the member reference variables</li> <li>Modification methods returning new objects with modified contents instead of actually modifying the original object</li> </ul> Some examples of immutable classes in JDK: String, Wrapper classes like Interger, Float, Short etc. <h2>Weak and Strong Immutability:</h2> The last point (5) in the above list of points talks about creating a copy of original object and then modifying its contents before returning it. If you are aware of <a href="http://www.javaexperience.com/java-cloning-tutorial/" title="Java Cloning Tutorial, Shallow Copy and Deep Copy">Shallow and Deep Cloning</a> then you must be aware of subtle issues with Shallow cloning where the member reference variables are shared among the original and copy object. This results in modification of member reference object whether it is modified by using the parent original or parent copy object. Similar to Shallow cloning , if we don’t create a completely new copy of original object then the reference variables could be shared among the two objects which we think are going to be immutable. As with cloning, we have different terminology for the two cases where we have completely independent immutable objects and objects with shared reference member objects. The terms are Strong Immutability and Weak Immutability. <h3>Weak immutability</h3> The following code example demonstrates who weak immutability is implemented: <ul> <li>This program can be run as it is to see the output</li> <li>Spare me for not adding javadoc and code comments</li> <li>Ideally Member and Immutable classes should be in different files. I added them as single file only to make sure that it is easy to run the program and see output.</li> </ul> 1 Output: <blockquote> Before Modify, im1 member is 10 After Modify, im1 member is 20 After Modify, im2 member is 20 </blockquote> <h3>Strong immutability</h3> The following code example demonstrates how strong immutability is implemented: Notice the difference in the statements in yellow background. 1 Output: <blockquote> im1 member is 10 im1 member is 10 im2 member is 20</blockquote>
Java Interview Question - Difference between final, finally and finalize
Differences between StringBuilder and StringBuffer in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments