1. Overview
In this tutorial, we'll look at the Lombok @AllArgsConstructor, @NoArgsConstructor, and @RequiredArgsConstructor annotations.
When we use these annotations, Lombok generates the constructors automatically for us.
2. @AllArgsConstructor For All Arguments Constructor
@AllArgsConstructor generates a constructor requiring an argument for every field in the annotated class.
So we have the Employee class with two fields:
@AllArgsConstructor
public class Employee {
private String name;
private int salary;
}
When we de-lombok the class, it becomes:
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
}
Here, Lombok generates a constructor requiring a value for all fields.
2.1. Static Factory Method for @AllArgsConstructor
@AllArgsContstructor also allows the creation of static factory methods using the staticName attribute:
@AllArgsConstructor(staticName = "of")
class Department {
private String location;
private String employeeNumber;
}
Consequently, Lombok makes the constructor private and then create a static factory method with the given name:
class Department {
private String location;
private String employeeNumber;
private Department(String location, String employeeNumber) {
this.location = location;
this.employeeNumber = employeeNumber;
}
public static Department of(String location, String employeeNumber) {
return new Department(location, employeeNumber);
}
}
Here, the of method calls the private constructor.
3. @NoArgsConstructor For No Argument Constructor
@NoArgsConstructor generates a default constructor with no parameters.
We have the following Employee class:
@NoArgsConstructor
public class Employee {
private String name;
private int salary;
}
When we look at the generated code, we see that Lombok adds a no-args constructor:
public class Employee {
private String name;
private int salary;
public Employee() {
}
}
3.1. Static Factory Method for @NoArgsConstructor
Similar to @AllArgsConstructor, we can create a static factory method for construction purposes using the @NoArgsConstructor annotation:
@NoArgsConstructor(staticName = "of")
class Department {
private String location;
private String employeeNumber;
}
4. @RequiredArgsConstructor for Final and @NonNull Fields
@RequiredArgsConstructor generates a constructor requiring an argument for the final and @NonNull fields.
We have an Employee class which has one final field, name:
@RequiredArgsConstructor
public class Employee {
private final String name;
private int salary;
}
In the final generated code, Lombok creates a constructor expecting value for the name field:
public class Employee {
private final String name;
private int salary;
public Employee(String name) {
this.name = name;
}
}
4.1. Static Factory Method for @RequiredArgsConstructor
We can also create a static factory method using @RequiredArgsConstructor:
@RequiredArgsConstructor(staticName = "of")
class Department {
private final String location;
private final String employeeNumber;
}
5. Calling the Super Constructor
Lombok can't call the super constructor unless it is a no-args constructor.
We'll start with the Citizen class:
public class Citizen {
private String country;
public Citizen(String country) {
this.country = country;
}
}
Note that Citizen doesn't have a no-args constructor.
Then we'll extend Citizen:
@AllArgsConstructor
public class Employee extends Citizen {
private String name;
private int salary;
}
Here, Employee extends Citizen and is annotated with @AllArgsConstructor.
As a rule, Java requires subclasses to call one of the superclass constructors during construction. Since Citizen has only one constructor expecting a single argument, we must have a constructor in Employee calling it:
public Employee(String name, int salary, String country) {
super(country);
this.name = name;
this.salary = salary;
}
This is one possible constructor. It defines parameters for country, name, and salary fields and then calls the super constructor. However, @AllArgsConstructor or other constructor annotations can't generate a constructor similar to this. Because Lombok can't call a super constructor that has arguments.
To conclude, if the superclass doesn't have a no-args constructor, Lombok can't generate any constructor in the subclass.
6. Conclusion
In this tutorial, we've investigated the @AllArgsConstructor, @NoArgsConstructor, and @RequiredArgsConstructor annotations that Lombok provides.
Finally, check out the source code for all examples in this tutorial over on Github.