Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

Java is an object-oriented language, which means we must deal with constructors while initializing objects.

Project Lombok can help us generate the constructors by simply using different annotations for different types of constructors and thus reduce the effort of writing boilerplate code.

In this tutorial, we’ll look at how the @RequiredArgsConstructor annotation can help us auto-generate the constructor for our classes.

2. Setup

To use Project Lombok, let’s add the dependency in our pom.xml file:


3. Usage

@RequiredArgsConstructor is a Lombok annotation that generates constructors for all final and non-null fields.

In our examples, we’ll define classes with differently-scoped member variables and see how the annotation behaves in different scenarios.

3.1. final Fields

final fields are the class members, which are initialized once before the constructor completes.

Let’s define a class with a final field and then test the @RequiredArgsConstructor annotation for the same:

public class ClassWithFinalMembers {

    private final String stringObject;

    // standard getters

The @RequiredAgrsConstructor annotation generates a parameterized constructor with the final member. Now we’ll execute a simple test to initialize the above class successfully:

void whenClassHasFinalMembers_thenGeneratedConstructorHasParameters() {
    ClassWithFinalMembers classWithFinalMembers = new ClassWithFinalMembers("dummyString");
    Assertions.assertEquals("dummyString", classWithFinalMembers.getStringObject());

3.2. Non-final Fields

When a class has non-final fields, the @RequierdArgsConstructor annotation doesn’t add them to the constructor parameters while generating the code.

If there are no final or non-null fields and only non-final fields, then we’ll get a constructor with no parameters generated.

Let’s see an example of the above scenario and subsequently validate the same with a simple test case:

public class ClassWithNonFinalMembers {

    private String stringObject;

    // standard getters
void whenClassHasNonFinalMembers_thenGeneratedConstructorHasNoParameters() {
    ClassWithNonFinalMembers classWithNonFinalMembers = new ClassWithNonFinalMembers();

We’ve defined a class with a non-final member, therefore using @RequiredArgsConstructor doesn’t add the member to the generated constructor parameters. If we try to use the parametrized constructor, the compiler throws an error.

3.3. Non-null Fields

To generate a constructor with non-null fields, we can use the @NonNull Lombok annotation:

public class ClassWithFinalNonNullMembers {

    private final String finalStringObject;
    private String nonNullStringObject;

    private String nonFinalStringObject;

    // standard getters

Now, let’s see a simple test case:

void whenClassHasFinalAndNonNullMembers_thenGeneratedConstructorHasParameters() {
    ClassWithFinalNonNullMembers classWithFinalNonNullMembers = new ClassWithFinalNonNullMembers("finalString", "nonNullString");
    Assertions.assertEquals("finalString", classWithFinalNonNullMembers.getFinalStringObject());
    Assertions.assertEquals("nonNullString", classWithFinalNonNullMembers.getNonNullStringObject());

We’ve defined a class with final, non-null, and non-final members. Therefore, the @RequiredArgsConstructor annotation generates the constructor, which initializes the non-null members in addition to the final ones.

4. Conclusion

In this article, we saw how @RequiredArgsConstructor generates the constructor to initialize the members of the class.

final members and members annotated as @NonNull are included in the constructor parameters and definition. However, the members which are neither final nor annotated with @NonNull aren’t initialized by the generated constructor.

As always, the source code for all the examples can be found over on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!