I love project Lombok but these days I'm reading and trying some of the new features of Java 14.
Inside the new capability, there is the record keyword that allows creating a class with already built-in the following functionality: constructor, private final fields, accessors, equals/hashCode, getters, toString methods.
Now my question is: is it better to rely on the feature of Lombok or should we start using the record functionality:
Is it better to use this:
record Person (String name, String surname) {}
or that:
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Person {
@Getter private int name;
@Getter private int surname;
}
What are the pros and cons of both approaches?
record
will not work for things expecting JavaBeans-style getters and setters. – Ghazialice.phoneNumber()
rather than the JavaBeans convention of prefixing withget
, as inalice.getPhoneNumber()
. – Passionalrecord Person (String name, String surname) {}
vs:@Value class Person { int name; int surname; }
More other, just change@Value
to@Data
and magically you'll have a non-immutable class compatible with ORMs. Something that is not possible or difficult to achieve with java 17 record. – Nucleoprotein