Lombok constructor
Lombok can generate a constructor for a class. We can choose to generate no argument constructor or constructor with all fields as arguments or constructor for some specific fields.
In this article, we will take a look at different constructor annotations such as @NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor to generate class constructors.
[the_ad id=”651″] Lombok @NoArgsConstructor
Use this annotation to generate a no argument constructor for a java class. This annotation is applied at class level. Example,
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class User {
} The generated constructor will not accept any arguments and will look as below
public User() {
} Lombok @AllArgsConstructor
This annotation will generate a constructor that accepts all the fields of the class as arguments. Example,
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class User {
private String name;
private int age;
} This will generate a constructor of below form
public User(String name, int age) {
} You can use @NoArgsConstructor and @AllArgsConstructor together. In this case, two constructors will be generated.
If any field is marked with @NonNull annotation, then the generated argument constructor will also have a null check for that parameter.
If the value of that field is null while calling the constructor, a NullPointerException will be thrown. Example,
import lombok.AllArgsConstructor;
import lombok.NonNull;
@AllArgsConstructor
public class User {
@NonNull
private String name;
private int age;
} Now, when an object of User is created with null value for name as
User user = new User(null, 22);
Below will be the result
Exception in thread “main” java.lang.NullPointerException: name is marked non-null but is null
Note that it is not possible to generate a constructor for some specific fields with @AllArgsConstructor.
[the_ad id=”656″] @RequiredArgsConstructor
This annotation generates a constructor for all those fields which are marked as @NonNull or final.
For all @NonNull and final fields, a single constructor will be generated, which accepts these fields.
So, this annotation can be used to generate constructor for some specific fields. Example,
import lombok.RequiredArgsConstructor
@RequiredArgsConstructor
public class User {
@NonNull
private String name;
private final int age;
private String gender;
} This will generate a constructor of below form
public User(String name, int age){
} Important:
If there are no @NonNull and final fields in a class, then @RequiredArgsConstructor will generate a constructor without any arguments.
In this case, if both @RequiredArgsConstructor and @NoArgsConstructor are applied over a class, then the compiler will raise an error as.
Duplicate method User() in type User
Reason is obvious.
Making private constructor
There may be a case where you need to write a private constructor so that the class can not be directly instantiated.
Lombok constructor annotations provide staticName attribute which takes a string value and generates a public static method with this name.
This public static method returns an object of the class. Example,
import lombok.NoArgsConstructor
@NoArgsConstructor(staticName="getUser")
public class User {
} Now, you can not create an object of this class as
User user = new User();
For creating an object, below syntax needs to be used
User user = User.getUser();
Note that this has nothing to do with singleton pattern and each time getUser() is called, a new object will be created.
[the_ad id=”644″] Hope the article was useful.