Creating Immutable Class

On May 16, 2016 by digvijayb |

How to create immutable class

Immutable Classes are those who’s object once created it can’t be change (or value can’t change). So to create Immutable Class in java there are some simple that are listed below.

Rules for immutable class:

  • No setter should provide for the field.
  • All field should be final and private.
  • Make the class final as an immutable class should not be inherited or an another for this could be to make the constructor private and construct instances in factory methods.
  • If there is any field which is a references to mutable objects make sure that it should not be modified :
    • Don’t provide methods that modify the mutable objects.
    • Don’t share references to the mutable objects. Always create a copy of the object and provide that for any operation.

Sample of Immutable Class

import java.util.Date

public final class ImmutableSample{

    private final int intField; // Is value type 
    private final String stringField;// Is Immutable
    private final Date dateField;// Is mutable

    private ImmutableSample(int intField, String stringField, Date dateField){
        this.intField = intField;
        this.stringField = stringField;
        this.dateField = new Date(dateField.getTime());
    }


    public static ImmutableSample getInstance(int intField, String stringField, Date dateField){
        return new ImmutableSample(fld1, fld2, date);
    }

    //Provide no setter methods

    /**
    * int is value type variable, So the value in intField going change every 
    * */
    public int getIntField() {
        return intField;
    }

    /**
    * String class is also immutable so we can return the instance variable as it is
    * */
    public String getStringField() {
        return stringField;
    }

    /**
    * As Date field is mutable so we create a copy of dateField. So that modification will not effect dateField.
    * */
    public Date getDateField() {
        return new Date(dateField.getTime());
    }

    @Override
    public String toString() {
        return intField +" - "+ stringField +" - "+ dateField;
    }
}

Some advantage of Immutable Object

  • They are thread-safe so don’t have and synchronization issues.
  • They can be best use in Map’s key and Set’s element.
  • It easier to parallelize your program as there are no conflicts among immutable objects.
  • They have their class invariant established once upon construction, and it never needs to be checked again

Please do comment and suggest

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts

Categories

Java 14

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Draftblog 1

Popular Tags

Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1) Draftblog (1)

About

DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc

Disclamer

This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.