Prefer Immutable Objects

Summary

Where possible, create objects that cannot be changed - especially if those objects will be long-lived or globally accessible.

Details

Mutable state makes programs harder to understand and maintain.

When objects are short-lived, and do not leave method scope, mutable state causes few problems. Writes and reads will be close together and there will be a clear order in which this happens.

For longer-lived objects, things are more complex.

If an object escapes from a method then it may be accessed from more than one location within the code.

We must start by assuming that anything that can happen to these objects will. We can only confirm that certain situations do not occur by examining the whole program.

The set of things that might happen to an immutable object is far smaller than for a mutable one. By constraining how long lived objects can behave we have made things simpler. There are fewer possibilities that we must consider.

Unfortunately, it is not always easy to tell from a class definition what the lifecycle of objects of that type will be. Perhaps only short-lived instances will be created. Perhaps only long-lived ones. Perhaps a mixture of the two.

If we design immutable classes by default we do not need to worry about this.

The Problem With Mutable Objects

If we have a very simple class such as Foo

  1. public class Foo {
  2. private Long id;
  3. public Long getId() {
  4. return id;
  5. }
  6. public void setId(Long id) {
  7. this.id = id;
  8. }
  9. @Override
  10. public int hashCode() {
  11. return Objects.hashCode(id);
  12. }
  13. @Override
  14. public boolean equals(Object obj) {
  15. if (this == obj)
  16. return true;
  17. if (obj == null)
  18. return false;
  19. if (getClass() != obj.getClass())
  20. return false;
  21. Foo other = (Foo) obj;
  22. return Objects.equals(id, other.id);
  23. }
  24. }

We would need to search our codebase for all usages of it to establish the following :

It is Never Accessed from Multiple Threads

Foo is not thread safe.

Writes to longs are not atomic and nothing within Foo itself establishes a happens-before relationship between the field write and read.

If setId and getId are ever called from different threads we might get back stale or garbage values.

setId Is Never Called After Foo Has Been Placed in a Set

The hashcode of this class relies on a mutable field. If we modify it after we place it in a set then our program will not behave as we expect.

The Flow of Our Data

Even if our program behaves correctly, we need to do work in order to understand how it functions.

setId can be called at any point after the object is created. We can, therefore, only understand how data flows through our program by looking for all calls to setId - perhaps there are several, perhaps there is only one. The only way we can discover this is by examining the entire program.

Immutable Objects

If we can make our objects immutable we gain guarantees that mean we do not need to worry about how our objects are used.

  1. @Immutable
  2. public final class Foo {
  3. private final Long id;
  4. public Foo(Long id) {
  5. this.id = id;
  6. }
  7. public Long getId() {
  8. return id;
  9. }
  10. @Override
  11. public int hashCode() {
  12. return Objects.hashCode(id);
  13. }
  14. @Override
  15. public boolean equals(Object obj) {
  16. if (this == obj)
  17. return true;
  18. if (obj == null)
  19. return false;
  20. if (getClass() != obj.getClass())
  21. return false;
  22. Foo other = (Foo) obj;
  23. return Objects.equals(id, other.id);
  24. }
  25. }

It no longer matters if Foo is long or short lived.

It is inherently thread-safe.

We know that whatever value we construct it with will remain until it dies. There is only one possible point where data is written so we do not need to search for others.

Annotations

The example uses the JSR3051 javax.annotation.concurrent.Immutable annotation.

This does not in any way change the object’s functionality but provides a way to communicate the intent of this being an immutable class. Static analysis tools such as Mutablility Detector can check if this intent has been violated.

We can tell at a glance that Foo is immutable as it has final fields of a well known immutable type.

The final keyword ensures only that the reference a field points to will not change.

If the field were of type Bar then we would not know if it were mutable or not without examining Bar to see if it too were immutable. Even if we were not using a static analysis tool the use of the Immutable annotation would make this assessment faster.

Instead of updating the state of immutable objects, we create new instances that retain the state we do not wish to modify.

This pattern seems strange to some Java programmers at first, but the programming model is similar to how the familiar String class works.

  1. @Immutable
  2. public final class Bar {
  3. private int anInt;
  4. private String aString;
  5. public Bar(int anInt, String aString) {
  6. this.anInt = anInt;
  7. this.aString = aString;
  8. }
  9. @CheckReturnValue
  10. public Bar withAnInt(int anInt) {
  11. return new Bar(anInt, this.aString);
  12. }
  13. @CheckReturnValue
  14. public Bar withAString(String aString) {
  15. return new Bar(this.antInt, aString);
  16. }
  17. }

Instances of Bar with new values can be obtained by calling withAString and withAnInt.

The JSR305 javax.annotation.CheckReturnValue enables static analysis tools such as Error Prone to issue a warning if a mistake is made such as in the code below.

  1. public Bar doThings(Bar bar) {
  2. if(someLogic()) {
  3. bar.withAnInt(42);
  4. }
  5. return bar;
  6. }

The call here to withAnInt achieves nothing because the return value is not stored. Most likely, the programmer intended to write:

  1. public Bar doThings(Bar bar) {
  2. if(someLogic()) {
  3. return bar.withAnInt(42);
  4. }
  5. return bar;
  6. }

When to Use Mutable Objects

Mutable objects require slightly less boilerplate to create than immutable ones.

If you know that a class will only ever be used to create short-lived, local objects then you might consider making it mutable. But you must weigh this against the additional work required to ensure that the class is only ever used in this fashion as the codebase grows.

Options exist to auto-generate both immutable and mutable classes, thereby removing mutable objects’ main advantage. Two of these options are discussed further in “Know How to Implement Hashcode and Equals”.

Mutable objects used to be the norm in Java. As a result, many common frameworks require mutable objects. Persistence and serialization frameworks often require Java beans with no args constructors and setters. Other frameworks might require you to use two-stage construction with a lifecycle method such as init.

It is not always highlighted in the documentation but some long standing frameworks have been updated to support immutable objects.

Jackson for example now allows constructors and factory methods to be annotated :-

  1. public class Foo {
  2. private final int x
  3. private final int y;
  4. @JsonCreator
  5. public Foo(@JsonProperty("x") int x, @JsonProperty("y") int y) {
  6. this.x = x;
  7. this.y = y;
  8. }
  9. }

Other frameworks, such as Hibernate, can only be used with classes that provide a default constructor. Although they can be configured to set fields directly without the need for setters this causes more problems than it solves.

If you are tied to a framework that requires mutability then you will need to use mutable objects where you interface with that framework.