What does it mean for a class to be immutable in Java?

Tremaine Eto
Apr 7 · 3 min read

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 final keyword

This means that all fields, or data members, must have the keyword final in front of them along with the previous section’s private.

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 value:

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?

In Java, there are many wrapper classes:

  • String
  • Integer
  • Boolean
  • Character
  • Short
  • Float
  • Double
  • Long
  • Byte

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.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store