Categories
Java

Records in Java

Lombok is dead?

This feature was introduced in Java 14 and allows you to define a simple data class, with an automatically generated constructor, accessors, equals, hashCode, and toString methods.

A record is defined by using the record keyword, followed by the class definition and the (fields) clause, which specifies the fields of the record.

Here’s an example of how to define a record:

record Person(String firstName, String lastName, int age) { }

In this example, the Person record is defined with three fields: firstName, lastName, and age.

This feature can be useful in situations where you want to define simple data classes, where the primary purpose of the class is to hold data, and you don’t need to add any additional behavior. The automatically generated methods make it easy to handle the state of the object, and it allows for less boilerplate code and more readable and maintainable code.

Leave a Reply

Your email address will not be published. Required fields are marked *