Immutable Class


Wikipedia states that

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

In simple words, we cannot change value of any field of  an immutable object after its creation. Consider the following simple Data Model class

public class Person
{
    private String firstName;
    private String lastName;
    private Address address;

    public Person(String firstName, String lastName, Address address)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getFirstName()
    {
        return firstName;
    }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getLastName()
    {
        return lastName;
    }
    public void setAddress(Address address)
    {
        this.address= address;
    }

    public Address getAddress()
    {
        return address;
    }
}

We can initialize object of Person class with following line of code

Person p = new Person ("Bilal", Mirza, new Address("Lahore", "Pakistan"));
//We supposed Address constructor with city and country name parameters

We cab also change fields of the object p by using its setters and getters, as follows

p.setFirstName("Hashim");
p.setLastName("Amla");
p.setAddress(new Address("Durban", "South Africa"));

For making objects immutable, we need to make following changes in its class structure.

  1. Declare the class as final to prevent it be extended.
  2. Declare all field as private and final.
  3. Remove all setter methods.
  4. If class contains reference of another object, return copy of this object in its getter method.

Following class is immutable version of above class

public final class Person
{
    private final String firstName;
    private final String lastName;
    private final Address address;

    public Person(String firstName, String lastName, Address address)
    {
       this.firstName = firstName;
       this.lastName = lastName;
       this.address = address;
    }

   public String getFirstName()
   {
      return firstName;
   }
    public String getLastName()
    {
        return lastName;
    }
    public Address getAddress()
    {
        return address.clone();
    }
}

See Also


Advertisements

Author: Bilal Mirza

I have been working as Software Engineer since September 2009. I have been enjoying Java and Web. I have experienced Struts1, Struts2, Hibernate, JavaFX, Jasper Reports, JQuery, and more. I did Intermediate and Graduation with major subjects Mathematics, Physics, and Computer Studies. I completed my M.Sc.(I.T.) from University of Punjab Lahore, Pakistan, and was awarded Gold Medal on getting first position.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s