Recent Tutorials and Articles
    Immutable Objects in Java
    Published on: 4th August 2013

    This tutorial explains immutable objects, lists some of the immutable objects in java library and finally details designing a class to ensure that all of its objects are immutable.

    Immutable Objects are the objects whose data can not be modified after the instance creation i.e. once the instance is created, none of its properties can be modified in any manner.

    There are many immutable objects in the java libraries of which some are as follows -

    1. String
    2. All the primitive data types wrapper classes - Byte, Short, Character, Integer, Long, Float, Double, Boolean
    Designing a class yielding immutable objects

    Following are the rules to design a class for immutable objects -

    1. All the fields must be final to ensure that fields can't be reassigned
    2. There must not be any getter methods for any of the mutable fields as clients can modify that field data thus breaking the immutability of that object
    3. We can have methods which seem to change the property of the object but they must return a new object with new data rather than modifying the current object

    Here is an example of a class for immutable objects

    Employee.java
    package com.sts.allprogrammingtutorials.javabasics;
    
    import java.util.Calendar;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class Employee {
    	
    	private final Long id;
    	private final String name;
    	private final String emailAddress;
    	private final Calendar dateOfBirth;
    	
    	public Employee(Long id, String name, String emailAddress, Integer year, Integer month, Integer date) {
    		this.id=id;
    		this.name = name;
    		this.emailAddress = emailAddress;
    		
    		final Calendar calendar = Calendar.getInstance();
    		calendar.set(year, month, date);
    		
    		this.dateOfBirth = calendar;
    	}
    
    	public Long getId() {
    		return id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getEmailAddress() {
    		return emailAddress;
    	}
    	
    	public Integer getYearofBirth() {
    		return dateOfBirth.get(Calendar.YEAR);
    	}
    	
    	public Integer getMonthofBirth() {
    		return dateOfBirth.get(Calendar.MONTH);
    	}
    	
    	public Integer getDateofBirth() {
    		return dateOfBirth.get(Calendar.DATE);
    	}
    	
    	/**
    	 * This method doesn't break immutablity as instead of modifying the object, 
    	 * it's returning a new object with the updated name.
    	 */
    	public Employee updateName(String newName) {
    		return new Employee(id, newName, emailAddress, getYearofBirth(), getMonthofBirth(), getDateofBirth());
    	}
    }
    
    

    As you can see that this class follows all the rules for designing a class yielding immutable objects.

    1. All the fields - id, name, emailAddress and dateOfBirth - are final and initialized in the constructor
    2. Getter methods are provided only for id, name and emailAddress as dateOfBirth is of Calendar type which is mutable. However, 3 getter methods(for year, month and date) are created to return information that might be needed by client
    3. It has got a method to update the name of an employee but internally that method is returning a new instance of Employee

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 4th August 2013

    Comment Form is loading comments...