Project Lombok Annotations With Examples

Project Lombok Annotations With Examples | It is an open-source Java API, used to generate code for constructors, set/get methods, toString, equals and hashCode etc.

To use Lombok API, we should enable/activate Lombok in STS/Eclipse then we can use Lombok API Annotations. Lombok Activation Steps:-

  • Download the Lombok Jar file.
  • Double-click to run the jar file, specify the STS/Eclipse IDE location => Click on the Install/Update Button.
  • Or, in the terminal/command prompt: java -jar lombok.jar => Click on the Install/Update Button.
Project Lombok Installation

In the Spring Starter project, while creating the project we can add the dependency for Lombok.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

Lombok Annotations

  • @Getter: It will generate get() method for every variable
  • @Setter: It will generate set() method
  • @ToString: Generates toString () method.
  • @EqualsAndHashCode: It will generate equals() and hashCode() methods. To compare two objects of the same class, we must generate equals() and hashCode().
  • @NoArgsConstructor: To generate default constructor.
  • @AllArgsConstructor: generate a constructor with all variables.

Lombok is going to work after your coding and before compilation only. Lombok Dependency/jar not required once compilation is done i.e. This JAR is not required in production/Deployment time. While compilation, it generates the required methods and the .class file that contain complete code with all those methods.

Java compiler provides a default constructor if no constructor exists in the code. But if other constructor exist then the Java compiler won’t provide the default constructor. In that case, we can use @NoArgsConstructor. If either Programmer or Lombok, defines any param constructor, first define default/zero args constructor, then any param constructor is fine.

Model class:-

package com.knowprogram.demo;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@NoArgsConstructor
public class Employee {
    private Integer empId;
    private String ename;
    private Double esal;
}

Runner class:-

package com.knowprogram.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class LombokRunnerTest implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        Employee emp = new Employee();
        emp.setEmpId(100);
        emp.setEname("Jerry");
        emp.setEsal(5000.0);

        System.out.println(emp.getEname()); // Jerry
        System.out.println(emp); 
        // Employee(empId=100, ename=Jerry, esal=5000.0)
    }

}
  • @RequiredArgsConstructor + @NonNull: If we want to create any specific params constructor then use these annotations.
  • @NonNull: It will select a variable for the constructor param.

Example-1:-

@RequiredArgsConstructor
class Emp {
    @NonNull
    Integer eid;

    String ename;

    Double esal;
}

Generated Code:-

class Emp {
    Integer eid;

    String ename;

    Double esal;

    Emp(Integer eid) {
        this.eid=eid;
    }
}

Example-2:-

@RequiredArgsConstructor
class Emp {
    @NonNull
    Integer eid;

    String ename;

    @NonNull
    Double esal;
}

Generated Code:-

class Emp {
    Integer eid;

    String ename;

    Double esal;

    Emp(Integer eid,Double esal) {
        this.eid=eid;
        this.esal=esal;
    }
}

Example-3:-

@NoArgsConstructor
@RequiredArgsConstructor
class Emp {
    @NonNull
    Integer eid;

    String ename;

    Double esal;
}

Generated Code:-

class Emp {
    Integer eid;

    String ename;

    Double esal;

    Emp() {
        super();
    }

    Emp(Integer eid) {
        super();
        this.eid=eid;
    }
}

Example-4:-

@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
class Emp {
    @NonNull
    Integer eid;

    String ename;

    Double esal;
}

Generated Code:-

class Emp {
    Integer eid;

    String ename;

    Double esal;

    Emp() {
        super();
    }

    Emp(Integer eid) {
        super();
        this.eid=eid;
    }

    Emp(Integer eid,String ename,Double esal) {
        super();
        this.eid=eid;
        this.ename=ename;
        this.esal=esal;
    }
}

The @NoArgsConstructor always generates the default constructor. For @RequiredArgsConstructor If there are no variables annotated with @NonNull then it will generate a default constructor.

@RequiredArgsConstructor
public class Employee {
    private Integer eid;

    private String ename;
}

Generated Code:-

public class Employee {
    private Integer eid;

    private String ename;

    public Employee() {
        super();
    }
}
@NoArgsConstructor
@RequiredArgsConstructor
public class Employee {
    private Integer eid;

    private String ename;
}

Now this code, we will get the compile time error:- duplicate method Employee(). Since there is no @NonNull field therefore @RequiredArgsConstructor creates a no-arg constructor and @NoArgsConstructor also creates a no-arg constructor. Therefore compile gives an error.

  • @AllArgsConstructor: It will generate a default constructor if the class has zero variables (no variables).
@AllArgsConstructor
public class Employee {
}

Generated Code:-

public class Employee {
    public Employee() {
        super();
    }
}
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
}

It will also gives compile time error:- duplicate method Employee() because both @NoArgsConstructor and @AllArgsConstructor creates no-args constructor.

@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
public class Employee {
}

It will also give compile time error:- duplicate method Employee() because
@NoArgsConstructor, @RequiredArgsConstructor, and @AllArgsConstructor creates no-args constructor.

@Data in Lombok

@Data reduces multiple annotations coding. @Data is a combination of
@Setter + @Getter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor.

@Data
public class Employee {
private Integer eid;
}

Generated Code:-

public class Employee {
    private Integer eid;

    public Employee() { ___ } //default const.

    // setEid, getEid

    // equals() hashCode()

    // toString()
}
@Data
public class Employee {
    @NonNull
    private Integer eid;
}

Generated Code:-

public class Employee {
    private Integer eid;
    // one param constructor

    public Employee(Integer eid) { this.eid=eid; }

    // setEid, getEid
    // equals() hashCode()
    // toString()
}

@Data provides internally @RequiredArgsConstructor, only if programmer did not write any Constructor annotation (@___ArgsConstructor).

Example-1:-

@Data // it wont provide @RequiredArgsConstructor now.
@NoArgsConstructor // default constrcutor
public class Employee {
    @NonNull
    private Integer eid;
}

Generated Code:-

public class Employee {
    private Integer eid;

    // only one constrcutor
    public Employee() { ___ }
}

Example-2:-

@Data
@AllArgsConstructor
public class Employee {
    @NonNull
    private Integer eid;

    private String ename;

    private Double esal;
}

Generated Code:-

public class Employee {
    private Integer eid;

    private String ename;

    private Double esal;

    public Employee(eid,ename,esal) { ___ } //3 params
}

Lombok supports only 3 constructors (1 default, 2 param). If we need any other constructors then we must add manually.

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@Data
@NoArgsConstructor // default constructor
@RequiredArgsConstructor // 1 param constructor
@AllArgsConstructor // 3 param constructor
public class Employee {

    @NonNull
    private Integer eid;

    private String ename;

    private Double esal;

    // if we want add one more const then we can add them manually
    public Employee(String ename) {
        this.ename=ename;
    }
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *