Using Lombok, is it possible to specify a one-argument constructor?
My intention is to use Lombok annotations to create a constructor such as the one below.
class MyClass { private String param; private Integer count; public MyClass(String param) { this.param = param; } }
Create a Static Factory Method with @AllArgsConstructor When staticName is used, Lombok marks the generated constructor as private and creates a static factory method - of() in our case - that will be used to construct all User objects.
Generates a constructor with required arguments. Required arguments are uninitialized final fields and fields with constraints such as @NonNull .
It generates a constructor that initializes all the final fields, as well as all the non-final fields with no initializer that have been marked with @NonNull , in order to ensure that the field is never null.
In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes a conversion constructor because such a constructor allows automatic conversion to the class being constructed.
Lombok doesn't let you to specify the fields exactly, but there are 3 annotations to choose from. With
@RequiredArgsConstructor class MyClass { private final String param; private Integer count; }
you can get it. An argument is required if it's not initialized inline and final
or @NonNull
.
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