Are Java Record Classes Truly Immutable?

Uğur Taş
Codimis
Published in
5 min readMay 27, 2022

--

Mac screen
Photo by Emile Perron on Unsplash

Why does java introduce record classes?

Main purposes of introducing record classes:

  1. Reducing boilerplate code for data transfer objects. Java developers almost always write boilerplate codes such as accessors, mutators, equals, hashcode, and toString methods for data transfer objects. Using Lombok can help avoid this issue, but it adds an external dependency
  2. Making immutable object creation easy and handy. To create immutable java classes, classes need to be declared as final, all fields should be private and final too, accessor methods should be used carefully, we should not provide mutator methods, etc.

If you don’t have a medium membership, you can use this link to reach the article without a paywall.

So, Record classes are created mainly because of those reasons. With record classes, java developers will be able to create immutable objects without boilerplate code. However, the question remains: “Are Java Record Classes Really Immutable?”.

--

--

Uğur Taş
Codimis

A software developer with a high passion for learning, improving skills, and sharing knowledge. My motto is “fail million times if you take lessons every time”