How to Make Immutable Object in Java

Connect with

Oracle JavaImmutable objects are simply objects whose state (the object’s data) cannot change after construction.

In general, classes in an application are designed to carry data and behavior. Sometimes a class may be designed in such a way that its instances can be used just as carriers of related data without any specific behavior. Such classes can be called data model classes and instances of such classes are referred to as data objects. In general, in java environment DTO (Data Transfer Object) can be made immutable to access in multithreaded environment so the immutable version of the DTO objects can safely be used in a multithreaded environment.

Important point to make a class Immutable

  • All instance variables (state/property) must be set in the constructor alone. No other method should be provided to modify the state of the object. The constructor is automatically thread-safe and hence does not lead to problems.
  • It may be possible to override class methods to modify the state. In order to prevent this, declare the class as final. Declaring a class as final does not allow the class to be extended further.
  • All instance variables should be declared final so that they can be set only once, inside the constructor.
  • 4. If any of the instance variables contain a reference to an object, the corresponding gettermethod should return a copy of the object it refers to, but not the actual object itself.

Which classes are Immutable?

Few common Java classes that are immutable, all of the java.lang package wrapper classes are immutable: String, Boolean, Byte, Character, Double, Float, Integer, Long, Short.

When to use immutable classes?

Custome.java


/**
 *
 */
 package com.mysoftkey.test;/**
 * This class is used as a DTO (Data Transfer Object) or Domain model object
 * which carries the Customer related data from one tier to another tier
 * in network.
 *
 * @author Ranjeet Jha
 *
 */
 public class Customer {
  // Property/State
   private final String name;
   private final Address address;
  //Constructor
   public Customer(String name, Address add) {
   this.name = name;
   this.address = add;
}

/**
 * @return the name
 */
 public String getName() {
 return name;
 }

/**
 * @return the address
 */
 public Address getAddress() {
 //return a copy of the Address object
 return (Address) address.clone();
}
}

Address.java


/**
 *
 */
 package com.mysoftkey.test;
import java.io.Serializable;

/**
 * This class is used as a DTO (Data Transfer Object) or Domain model object
 * which carries the address related data from one tier to another tier in
 * network. This object can be cloned as {@link Cloneable} interface is implemented.
 *
 * @author ranjeet.kr@gmail.com
 *
 */
 public class Address implements Serializable, Cloneable {

 // State
 private String line1;
 private String line2;
 private String city;
 private String state;
 private String country;
 private String zipCode;

/**
 * @return the line1
 */
 public String getLine1() {
   return line1;
 }

/**
 * @return the line2
 */
 public String getLine2() {
   return line2;
 }

/**
 * @return the city
 */
 public String getCity() {
 return city;
 }

/**
 * @return the state
 */
 public String getState() {
 return state;
 }

/**
 * @return the country
 */
 public String getCountry() {
 return country;
 }
/**
 * @return the zipCode
 */
 public String getZipCode() {
 return zipCode;
 }

/**
 * @param line1
 *            the line1 to set
 */
 public void setLine1(String line1) {
 this.line1 = line1;
 }

/**
 * @param line2
 *            the line2 to set
 */
 public void setLine2(String line2) {
 this.line2 = line2;
 }

/**
 * @param city
 *            the city to set
 */
 public void setCity(String city) {
 this.city = city;
 }

/**
 * @param state
 *            the state to set
 */
 public void setState(String state) {
 this.state = state;
 }

/**
 * @param country
 *            the country to set
 */
 public void setCountry(String country) {
 this.country = country;
 }

/**
 * @param zipCode the zipCode to set
 */
 public void setZipCode(String zipCode) {
 this.zipCode = zipCode;
 }

/*
 * (non-Javadoc)
 * @see java.lang.Object#clone()
 */
 public Object clone() {
 try {
 return super.clone();
 } catch (CloneNotSupportedException e) {
 throw new Error("This should not occur since we implement Cloneable");
 }
 }
}

Your comment is welcome to improve this post. 🙂 cheers happy learning.


Connect with

12 thoughts on “How to Make Immutable Object in Java

  1. this is good demo of making a immutable object in java , understand this quickly. thanks a lot to explain easily …

Leave a Reply

Your email address will not be published. Required fields are marked *