Immutable objects

Most of the objects in Java are mutable. It means that their state/fields can be changed after the object was created. The examples of those are: ArrayList, Calendar, StringBuilder.

Immutable objects

An object is immutable if its state/fields cannot be changed after the object is constructed. In practice it means that:

  1. The object class must not have any mutable methods like setters.
  2. Every object contained within this object is also immutable (or otherwise protected from modification).
  3. If the object contains collection, it must be impossible to add new elements, remove existing elements or modify the value of any element. Every element of the collection must be immutable (or otherwise protected from modification).
  4. Every field of the object is final and private.
  5. The object class is declared as final.

The most notable examples of immutable classes are String, Integer and other wrappers of basic types, LocalDateTime and other classes from Java 8 Date and Time API.

Benefits of immutable objects

Reliance on immutable objects is widely accepted strategy for creating simple and reliable code. Few of the most notable benefits of immutable objects are listed below:

  1. These are simpler to construct, test, use and easier to reason about because they can never be changed. Class invariant is checked once and never changed. There is no need for defensive copying.
  2. Immutable objects are excellent candidates for Map and Set keys because these cannot change value while in the collection. Immutable objects prevents key modification.
  3. The internal state of the object stays consistent even after exception is thrown. This is not the case for many mutable objects.
  4. Immutable objects can be easily and safely cached because these are not going to change.
  5. Immutable objects are automatically thread-safe so the whole problem of thread synchronization is gone. Additionally, these are particularly useful in concurrent application.

Cost of immutable objects

Immutability may have a performance cost because completely new object needs to be created as opposed to updating an existing object in place. But often this cost is overestimated and can be reduced by the elimination of thread synchronization, easier/faster garbage collection and specialized builders.

Advertisements

About Robert Piasecki

Husband and father, Java software developer, Linux and open-source fan.
This entry was posted in Java, Software development practices and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.