Why does java introduce record classes?
Main purposes of introducing record classes:
- 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
- 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?”.