Announcing Stack Overflow Documentation



Sign up and start helping → Learn more about Documentation →

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?

share|improve this question
There is a introduction for immutable class: javapractices.com/topic/… – qrtt1 Jul 5 '10 at 4:23
check this @Immutable annotation from jcabi-aspects – yegor256 Sep 3 '13 at 9:20
That is 3 questions, not 1. – Raedwald Jan 2 '15 at 19:19
up vote 75 down vote accepted

What is an immutable object?

An immutable object is one that will not change state after it is instantiated.

How to make an object immutable?

In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters.

The following class will create an immutable object:

class ImmutableInt {
  private final int value;

  public ImmutableInt(int i) {
    value = i;

  public int getValue() {
    return value;

As can be seen in the above example, the value of the ImmutableInt can only be set when the object is instantiated, and by having only a getter (getValue) the object's state cannot be changed after instantiation.

However, there must be care taken that all objects that are referenced by the object must be immutable as well, or it could be possible to change the state of the object.

For example, allowing an reference to an array or ArrayList to be obtained through an getter will allow the internal state to change by changing the array or collection:

class NotQuiteImmutableList<T> {
  private final List<T> list;

  public NotQuiteImmutableList(List<T> list) {
    // creates a new ArrayList and keeps a reference to it.
    this.list = new ArrayList(list); 

  public List<T> getList() {
    return list;

The problem with the above code is, that the ArrayList can be obtained through getList and be manipulated, leading to the state of the object itself to be altered, therefore, not immutable.

// notQuiteImmutableList contains "a", "b", "c"
List<String> notQuiteImmutableList= new NotQuiteImmutableList(Arrays.asList("a", "b", "c"));

// now the list contains "a", "b", "c", "d" -- this list is mutable.

One way to get around this problem is to return a copy of an array or collection when called from a getter:

public List<T> getList() {
  // return a copy of the list so the internal state cannot be altered
  return new ArrayList(list);

What is the advantage of immutability?

The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.


share|improve this answer
+1 for the clarity and level of detail. Great answer. – jer Jul 2 '10 at 1:36
Thread safety (important for concurrency) is not the only advantage of immutability; it also means you don't have to make defensive copies of objects, and it prevents bugs because you can't by mistake modify objects that are not supposed to be modified. – Jesper Jul 2 '10 at 7:00
Instead of returning a copy of the list, you can also return Collections.unmodifiableList(list); to return a read-only view on the list. – Jesper Jul 2 '10 at 7:01
The class has to be made final too. Otherwise it can be extended with a setter method (or other kinds of mutating methods and mutable fields). – Abhinav Sarkar Jul 3 '10 at 20:19
@AbhinavSarkar Doesn't have to be final if the class only has private fields as they cannot be accessed from subclasses. – icza Sep 3 '14 at 13:46

In addition to the answers already given, I'd recommend reading about immutability in Effective Java, 2nd Ed., as there are some details that are easy to miss (e.g. defensive copies). Plus, Effective Java 2nd Ed. is a must-read for every Java developer.

share|improve this answer
This is the exact resource to look at. The benefits that are mentioned range right from less error prone code to thread safety. – gpampara Jul 2 '10 at 5:37

You make a class immutable like this:

public final class Immutable
    private final String name;

    public Immutable(String name) 
        this.name = name;

    public String getName() { return this.name; } 

    // No setter;

Immutable classes are useful because they're thread-safe. They also express something deep about your design: "Can't change this." When it applies, it's exactly what you need.

share|improve this answer

Immutability can be achieved mainly in two ways:

  • using final instance attributes to avoid reassignment
  • using a class interface that simply doesn't allow any operation that is able to modify what is inside your class (just getters and no setters

Advantages of immutability are the assumptions that you can make on these object:

  • you gain the no-side effect rule (that is really popular on functional programming languages) and allows you to use objects in a concurrent environment easier, since you know that they can't be changed in a atomic or non atomic way when they are used by many threads
  • implementations of languages can treat these objects in a different way, placing them in zones of memory that are used for static data, enabling faster and safer use of these objects (this is what happens inside the JVM for strings)
share|improve this answer
Immutable is not the same as equal side effect free. For example, an immutable object could produce side effects such as logging to a file. It is slightly inaccurate to say making an object immutable also makes it side effect free. – Grundlefleck Jul 18 '10 at 20:14
@Grundleflek, I think this may be splitting hairs. The class isn't immutable if it modifies a log file as part of its contract and that log file is accessible to other classes. If the log file is hidden from other classes, and not part of the class's contract then the class is effectively immutable and really for all intents and purposes side-effect free. The (unsourced) introduction on the Wikipedia side effect page reads "...an expression is said to have a side effect if, in addition to producing a value, it also modifies some state or has an observable interaction with calling functions." – Jeff Axelrod Jun 23 '11 at 3:05

Immutable classes cannot reassign values after it is instantiated.

to be immutable,.

  • Al the variables should be private.
  • No mutator methods(setters) are provided.
  • Avoid method overriding by making class final(Strong Immutability) or methods final(Week immutability).
  • Clone deeply if it contain non primitive or mutable classes.

* Strong immutability - by making class final
public final class TestImmutablity {

// make the variables private
private String Name;

//assign value when the object created
public TestImmutablity(String name) {
this.Name = name;

//provide getters to access values
public String getName() {

return this.Name;

Advanteges: Immutable objects contains its initialized values until it dies.

share|improve this answer

Q1) What is an immutable class and Object?

Ans) Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class

Q2) How to create an immutable class?

Ans) To create an immutable class following steps should be followed:

  • Create a final class.

  • Set the values of properties using constructor only.

  • Make the properties of the class final and private

  • Do not provide any setters for these properties.


    • Don't provide methods that modify the mutable objects.


share|improve this answer

Your Answer


