Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Project Lombok @Data annotation create a constructor of any kind?

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.

like image 528
JoseHdez_2 Avatar asked Feb 01 '17 12:02

JoseHdez_2


People also ask

Does @data create constructor?

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.

What does @data annotation do in Lombok?

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.

Does Lombok create constructor?

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).

What is the difference between @data and @value in Lombok?

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.


1 Answers

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).

like image 134
JoseHdez_2 Avatar answered Sep 21 '22 18:09

JoseHdez_2