I have a class with a @Data
annotation, but I'm not sure whether a constructor with arguments is generated or the only generated constructor is the default (no arguments) one from vanilla Java.
It is creating the constructor with zero parameters because @Data gives you Setters and Getters for your properties and you can call setX and setY after creating your instance. You can either make x and y @NonNull or final so they must be passed through the constructor or annotate your class Z with @AllArgsConstructor.
The @Data annotation does the following work: It generates the getter methods for all the fields. It generates the setter methods for all the non-final fields. It generates the toString() method implementation.
With Lombok, it's possible to generate a constructor for either all class's fields (with @AllArgsConstructor) or all final class's fields (with @RequiredArgsConstructor).
The big difference between @Value and @Data annotation is that @Value is mainly used to create immutable objects. @Value is a also an all-in-one annotation that combines: @Getter, @AllArgsConstructor, @ToString and @EqualsAndHashCode and @FieldDefaults(makeFinal = true, level = AccessLevel.
A @RequiredArgsConstructor
will be generated if no constructor has been defined.
The Project Lombok @Data page explains:
@Data is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructor exists).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With