When you think of immutability in Java, there are a core set of concepts that should come to mind.
In this article, I’ll enumerate some of the major points, and hopefully you’ll have a better idea of what it takes to be immutable in Java.
Immutable means its state can’t change
Immutability in Java at a high level essentially means what it does in English. In other words, something that is immutable in Java can’t change.
In Java, that means that the content or state of an immutable object simply can’t be changed. The language won’t allow it.
The class should be declared as final
The reason for this is because in Java, classes can be extended. The
final keyword in the class declaration literally makes it such that the class can’t be extended. In other words, it can’t change.
All fields in the class must have the private keyword
This means that instead of simply declaring the variable, the
private keyword needs to precede the variable.
This is so that the variables can’t be directly accessed. The ability for direct access would put the immutability at risk because then the accessor would be able to change it.
All fields in the class must have the
This means that all fields, or data members, must have the keyword
final in front of them along with the previous section’s
final makes it such that once the variable’s value is declared, it can’t change. This is the same keyword as the
final in the class declaration and works much the same.
All fields in the class must be initialized with a constructor that performs a deep copy
This isn’t as bad as it sounds.
In the class, you simply have to make a constructor (which has the name of the class along with its data members as parameters) that assigns the data members to its
This makes it such that the data members can’t accidentally be modified by object reference.
All getter methods need to perform a deep copy of mutable objects
When getters are in the class, they have to perform a deep copy using
clone() in Java for any objects that are mutable.
Let’s see what that would look like for our
Dog class I’ve been using as an example.
Hmm. That doesn’t look very different.
The thing here is that
String is already an object that is immutable by default in Java. You don’t have to worry about deep copying it because by definition in Java, it can’t be changed.
Well, it can, but if you change its value, Java creates a new object to store the new value behind the scenes. The original object never changes.
Anyways, back to
String being immutable. What else is immutable by default in Java?
String, Integer, Boolean, and wrapper classes are immutable
In Java, there are many wrapper classes:
In Java, these are all considered immutable, so you don’t need to worry about deep copying them in any immutable classes you create.
Immutable classes can’t have setters
The setter methods that you see in mutable classes just can’t be in immutable classes. After all, our definition of immutable in Java is dependent on the objects not being able to be changed. To set something that can’t be changed doesn’t make sense, and thus you can easily remember that setters should be off the table when it comes to an immutable class.