How to Use the @AllArgsConstructor Annotation of Project Lombok

How to Use the @AllArgsConstructor Annotation of Project Lombok

This article explains how to make use of the @AllArgsConstructor annotation of Project Lombok.

This post will focus on how to make use of the @AllArgsConstructor annotation of Project Lombok. The Lombok Project is a java library that helps a developer generate boilerplate code. By simply adding the Lombok library to your IDE and build path, the Lombok library will auto-generate the Java bytecode, as per the annotations, into the class files.

The @AllArgsConstructor annotation is added to a class and generates a constructor with a parameter for every field within the class. Fields marked with @NonNull result in null checks on those parameters.

Article Series

This article forms part of a multi-part series on how to use the Project Lombok to generate boilerplate code in java projects.

Define Maven Dependencies

The following dependencies should be included in the pom.xml file.

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.8</version>
  <scope>provided</scope>
</dependency>

Example: @AllArgsConstructor

The @AllArgsConstructor annotation is added to a class and generates a constructor with a parameter for every field within the class. Fields marked with @NonNull result in null checks on those parameters. Look at the sample code below to see how this works.

Origial Class

@AllArgsConstructor
@ToString
public class AllArgsConstructorExample01 {

    private long id;
    private String firstName;
    private String lastName;

    public static void main(String[] args) {
        AllArgsConstructorExample01 example01 = new AllArgsConstructorExample01(1, "Joe", "Soap");
        System.out.println(example01);
    }
}

To truly appreciate the magic of the Lombok library, you should compile the AllArgsConstructorExample01 class by making use of maven to build your project. As part of the compile process, Lombok will generate the boilerplate code depending on the type of annotation you used. To see the boilerplate code, you should decompile AllArgsConstructorExample01.class file. An easy way to do this is to make use of the following URL:

Java Decompiler: http://www.javadecompilers.com

Decompiled Class

public class AllArgsConstructorExample01 {
    private long id;
    private String firstName;
    private String lastName;
    
    public static void main(final String[] args) {
        final AllArgsConstructorExample01 example01 = new AllArgsConstructorExample01(1L, "Joe", "Soap");
        System.out.println(example01);
    }
    
    public AllArgsConstructorExample01(final long id, final String firstName, final String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    @Override
    public String toString() {
        return "AllArgsConstructorExample01(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ")";
    }
}

Summary

Congratulations !!! You have successfully used the @AllArgsConstructor annotation of Project Lombok. Please look out for more examples on how to make use of Project Lombok to simplify your Java coding experience. Follow me on any of the different social media platforms and feel free to leave comments.