Immutable Classes in Java

Immutability is one of the most important topics in programming and software design. Immutable classes are the classes which object cannot be changed once it is constructed. In other words, if you modify the immutable object it will result in the creation of a new immutable object.

For example: when we modify string object in Java it creates a new object and assign back to reference variable rather than modifying the original object. Hence, String class is an immutable class in Java. Let us understand how to make a class immutable in Java.

Example to make a class immutable in Java

Below are some of the rules that should be followed while defining any class immutable in Java.

  1. All the fields of an immutable class should be final and private.
  2. Do not allow any method to change the state of the object. For example: setXxx
  3. Make a class "final" so that subclasses cannot override its methods.
  4. All the mutable objects passed to constructor should not be directly used, instead you need to create a new object and assign it inside the constructor.
  5. All the mutable objects returned by the getter method must return a new copy of mutable object instead of returning the same object outside.

Now let us create an immutable class using above rules.

import java.util.ArrayList;
import java.util.List;

public final class ImmutableDemo {
	private final String name;
	private final List values;
	
	public ImmutableDemo(String name, List values) {
		this.name = name;
		this.values = copyList(values);
	}
	public String getName() {
		return name;
	}
	public List getValues() {
		return copyList(values);
	}
	private List copyList(List values) {
		List newList = new ArrayList();
		for ( String element : values )
			newList.add(element);
		return newList;
	}
}

Let us examine above code snippet:

  • First of all, all the fields declared in the class has been defined private and final.
  • The class is declared final so that it cannot be extended by any other class.
  • Mutable object passed to constructor issued to create a new object and then assigned to a class variable.
  • Mutable object returned from getValues() method first creates a new object that should be returned from the method.

Example of Some of the Predefined Immutable Class in Java

Some of the most commonly used predefined immutable classes in Java are listed below:

  • java.lang.String
  • Wrapper Classes: Integer, Byte, Character, Float, Long, Double, Short
  • java.util.Locale
  • java.net.URL
  • java.util.UUID

Benefits of Immutability

As we know once constructed, the state of immutable objects cannot be changed. Hence, you can cache the values without worrying about the values becoming stale; they are best map keys and inherently thread-safe.

  • Immutable objects are thread safe.
  • As the objects can be easily cached, it improves the performance.
  • It is good to be used as keys in HashMap.
Article tagged as
Author
Author: Amit Gupta
Published On: 06/09/2015
Last revised On: 13/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media