Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a one-argument constructor using Lombok?

Tags:

java

lombok

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;     } } 
like image 649
barbara Avatar asked Dec 18 '14 12:12

barbara


People also ask

How do you make a constructor in Lombok?

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.

What is required args constructor Lombok?

Generates a constructor with required arguments. Required arguments are uninitialized final fields and fields with constraints such as @NonNull .

Does Lombok data create constructor?

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.

What is a one argument constructor?

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.


1 Answers

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.

like image 169
maaartinus Avatar answered Sep 22 '22 15:09

maaartinus