I love project Lombok but in 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 better to rely on the feature of Lombok or should we start using the record functionality:
Is 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 the both approach?
record
will not work for things expecting JavaBeans-style getters and setters.alice.phoneNumber()
rather than the JavaBeans convention of prefixing withget
, as inalice.getPhoneNumber()
.record 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.