@AllArgsConstructor vs @NoArgsConstructor vs @RequiredArgsConstructor – Lombok

Lombok Annotations For Generating Constructors.

In this post, we are going to look at the differences between the Lombok annotations stated above for automatically generating constructors in our Java application.

1. @NoArgsConstructor:

This annotation helps us to generate a constructor without any arguments in other words, it’s used to generate an empty constructor for our entity class. example.

1.1 Creating our entity class without the Lombok annotation:

public class Student{

    private String name;
    private String email;

    public Student() {
    }

   public Student( String name, String email){
  //
//
 }


1.2 Creating our entity class with the Lombok annotation:

@NoArgsConstructor
class Student{

    private String name;
    private String  email;

 public Student( String name, String email){
  //
//
}
}

Notice that in listing 1.1, we explicitly generated the empty constructor ( no argument constructor ) for our POJO class because we have a parameterized constructor in that class. Remember that in Java, if our POJO class contains a parametrized constructor, then we have to explicitly generate an empty constructor in that class just as we have done. But by using the Lombok @NoArgsConstrutor, we don’t have to explicitly generate that empty constructor. in listing 1.2.

2. @AllArgsConstructor:

This annotation helps us to generate a parameterized constructor that contains every non-static field in our POJO class ( both final and non-final fields will be included in the constructor ). example.

@AllArgsConstructor
class Student{

    private String firstName;
    private final String lastName;
    private String email; 
    private static  int  rollNumber;

}

If we de-lombok this class, we are going to see it as shown below.

public class Student{

    private String firstName;
    private final String lastName;
    private String email; 
    private static  int  rollNumber;

    public Student(String name, String lastName, String email) {
         this.firstName= frstName;
         this.lastName = lastName;
         this.email= email;
    }
}


Notice that the “rollNumber” of the student is not included in the constructor. That’s because it’s a static field.

3. @RequiredArgsConstructor

This annotation generates a parameterized constructor that contains only the final or @NonNull fields as arguments. example:

@RequiredArgsConstructor
public class Student{

    private String firstName;
   @NonNull
    private String lastName;
    private final String email;
    private static  int  rollNumber;



If we de-lombok this class, this is what we will see.

public class Student{

    private String firstName;
   @NonNull
    private String lastName;
    private final String email;
    private static  int  rollNumber;    

    public Student(String lastName , String email) {      
        this.email= email;
        this.lastName = lastName;
    }

Notice that the ” firstName ” and the rollNumber are NOT included in the constructor that’s because the firstName field is a non-final and the rollNumber is a static field.

Conclusion.

In this post, we have seen that the @RequiredArgsConstructor  will only generate a constructor that contains all final fields and all fields that are annotated with the @NonNull. The @AllArgsConstructor will only generate a constructor that contains all fields that are not static in our class. And the @NoArgsConstructor will generate a constructor with no arguments.

Happy coding …