“We humans always want more than what we have”. And that is the biggest reason behind the invention of new technologies.

If you are a java developer, POJO is not a new term for you. We have been using POJOs (Plain Old Java Objects) since long with various frameworks like hibernate, struts, spring etc. or even in the application without using a framework.

While writing a POJO it is a common practice to have various possible combinations of constructors, getters and setters methods, toString method, equals and hashcode method etc.

Long back when our IDEs were not so smart, we used to write all these elements at our own, but thanks to the smart IDEs available now a days like eclipse, STS,intellij etc. , it is no more our botheration to write these all. Just own a single click your IDE will generate everything for you.

But still for every POJO in your project, you need to instruct your IDE for generating the elements you want. May be you want to have only setter for a member(s) or maybe you want to have getter only for your particular member or you may want both for particular member(s).

Project Lombok aims to reduce the prevalence of some of the worst offenders by replacing them with a simple set of annotations.

For example , suppose you have are writing a class named Employee with the fields empCode, empName and designation and you want to have getters/setters for all fields. Lombok will generate getter and setters for you automatically, you just need to put just annotation @Getter for generating getter methods and @Setter for generating the setter methods.

import lombok.Getter;

import lombok.Setter;

@Getter

@Setter

public class Employee {

private int empCode;

private String empName;

private String designation;

}

Just by putting @Getter and @Setter annotations on class level you have included getter and setter methods for all fields.

Project Lombok gave us many annotations like these. We will have a detailed discussion on all annotations after going through installation phase.

Installation of Lombok :

Project Lombok is available as a single jar file on the project site: https://projectlombok.org/download.

After downloading the lombok.jar you just need to run it. You can run it just by double clicking the file or alternatively you can run it from command prompt also just by typing command — java -jar lombok.jar

The installer will attempt to detect the location of a supported IDE. If it cannot correctly determine where the IDE is installed, the location can be specified manually. Simply click “Install/Update” and IDE integration is complete.

The jar file will still need to be included in the classpath of any projects that will use Project Lombok annotations. Maven users can include Lombok as a dependency by adding this to the project pom.xml file:

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.16.20</version>

<scope>provided</scope>

</dependency>

Main Annotations From Lombok

1) @Getter And @Setter

@Getter and @Setter can be used on class level as well as field level also. If you use it with a class , getter and setters for all the fields will be generated. If you want to used it for selected fields only , you can use it with field also.

@Getter

@Setter

public class Employee {

private int empCode;

private String empName;

private String designation;

}

In the above example getter and setter will be generated for all three fields.

public class Employee {

@Setter

@Getter

private int empCode;

@Getter

private String empName;

@Getter

private String designation;

}

In the above example getter and setter both will be generated for empCode and for empName and designation only getter will be generated.

2) @AllArgsConstructor

This annotation generates an all-args constructor. An all-args constructor requires one argument for every field in the class.

@AllArgsConstructor

public class Employee {

private int empCode;

private String empName;

private String designation;

}

In the above example the class Employee is now having aconstructor to initialize all the members.

3) @NoArgsConstructor

This annotation will generate a zero arg constructor to your cllass.

@AllArgsConstructor

@NoArgsConstructor

public class Employee {

private int empCode;

private String empName;

private String designation;

}

In the above example the class Employee is now having two constructors one for initializing all fields and one with zero args.

4) @ToString

This annotation will generate an implementation of the toString() method. By default, it’ll print your class name, along with each field, in order, separated by commas.

@ToString

public class Employee {

private int empCode;

private String empName;

private String designation;

}

In the above example the class Employee is not having an overridden toString method which will print the empCode,EmpName and designation of the invoking object.

5) @EqualsAndHashCode

This annotation willgenerate implementations of the equals(Object other) and hashCode() methods. By default, it’ll use all non-static, non-transient fields to compare the objects.

@AllArgsConstructor

@NoArgsConstructor

public class Employee {

private int empCode;

private String empName;

private String designation;

}

Here we have created a class Employee without implementing equals and hashCode methods. Therefore this class is using the equals and hashCode implementation of object class.

public class EmployeeUser {

public static void main(String[] args) {

Employee emp1=new Employee(111,”AAA”,”SSE”);

Employee emp2=new Employee(111,”AAA”,”SSE”);

System.out.println(emp1.equals(emp2)); //will give false

}

}

In the above example we comparing the two objects of Employee class using equals method of object class, though the members are same but still the object class equals method will give you false.

But after adding EqualsAndHashCode annotation to Employee class, the same code will return true.

@AllArgsConstructor

@EqualsAndHashCode

public class Employee {

private int empCode;

private String empName;

private String designation;

}

Now the Employee class is having its own implementation of equals and hashCode method.

public class EmployeeUser {

public static void main(String[] args) {

Employee emp1=new Employee(111,”AAA”,”SSE”);

Employee emp2=new Employee(111,”AAA”,”SSE”);

System.out.println(emp1.equals(emp2)); //will give true

}

}

The above code will compare emp1 and emp2 and will return true. Because the equals method used now does not belongs to Object class rather Employee class has got its own implementation.

6) @Data

If you wish to include many things using just a single annnotation you can go for @Data. A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields. When you use @Data on a class automatically toString, equals, hashCode, getters, setters etc. all generated.

@Data

public class Employee {

private int empCode;

private String empName;

private String designation;

}

In the above example just by adding @Data we have generated many elements together.

Following code is using Employee class to check @Data.

public class EmployeeUser {

public static void main(String[] args) {

Employee emp1=new Employee();

//setter methods

emp1.setEmpCode(111);

emp1.setEmpName(“John”);

emp1.setDesignation(“SSE”);

//getter methods

System.out.println(emp1.getEmpCode());

System.out.println(emp1.getEmpName());

System.out.println(emp1.getDesignation());

//toString method

System.out.println(emp1);

//One more Employee object with same values to check equals

Employee emp2=new Employee();

emp2.setEmpCode(111);

emp2.setEmpName(“John”);

emp2.setDesignation(“SSE”);

System.out.println(emp1.equals(emp2));

}

}

A SSi Digital initiative to provide a perfect platform of learning & practice code through quality content delivery & hands-on