How to Use Lombok to remove boilerplate setters getters in Java

Hello Friends,

One of the points which is said time and again against Java is that ,we have to write a lot of boilerplate
code in the form of setters and getters for our simple POJO classes which unnecessarily increases the
length of our code.

To tackle this problem, there is an open source project called Project Lombok which solves this problem
by allowing you to just write few annotations and it will generate getters and setters in the .class which
are generated from Java files.


So....

In this quick tutorial, we will see how we can use Lombok library to get rid off the setters and getters which
we need to write in our POJO classes with only properties and no logic.

To understand how Lombok is helping us, we will divide this post into two parts :

1. Create a POJO without Lombok and add setters getters manually
2. Create POJO and use Lombok library to add getters setters

1. Create a POJO without Lombok and add setters getters manually

Let us create class for Employee with few properties as below without Lombok and name it  as
"EmployeeWithoutLombok":
package com.blogspot.javasolutionsguide.model;

public class EmployeeWithoutLombok {

    private int id;
    private String firstName;
    private String lastName;
    private int age;
    private String department;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}
As we can see above that we have to write setters and getters for all the properties.
Now to test this class ,let us write a test class with name "TestEmployeeWithoutLombok".
package com.blogspot.javasolutionsguide.lombokTest;

import com.blogspot.javasolutionsguide.model.EmployeeWithoutLombok;

public class TestEmployeeWithoutLombok {

    public static void main(String[] args) {
        EmployeeWithoutLombok employeeWithoutLombok = new EmployeeWithoutLombok();
        employeeWithoutLombok.setFirstName("Gaurav");
        employeeWithoutLombok.setLastName("Bhardwaj");

        System.out.println("Employee First Name:" + employeeWithoutLombok.getFirstName() + "\n" +
                "Employee Last Name:" + employeeWithoutLombok.getLastName());
    }
}

Output :
Employee First Name:Gaurav
Employee Last name:Bhardwaj

So here basically we are using setters and getters which we have written ourselves in the
"EmployeeWithoutLombok" class.

2. Create POJO and use Lombok library to add getters setters

To use Lombok, We need to :
- Add Lombok dependency in our pom.xml
- Install Lombok in our eclipse
- Add @Getters, @Setters annotation on our POJO
- Create a Test class to test setters and getters

Add Lombok dependency in our pom.xml


Let us first add the dependency of Lombok in our maven POM.xml(See highlighted part in below XML). 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blogspot.javasolutionsguide</groupId>
    <artifactId>lombakTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>lombakTest</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Install lombok  in our eclipse
Lombok jar comes with an installer. We just need to go to the folder where we have Lombok jar and run the
jar using below command.

java -jar lombok-1.18.4.jar


Once we have executed above command, Lombok will detect all the IDEs on our machine, like I have
eclipse:




Click on Install/Update and it will install Lombok in the Eclipse.

We can double check if our Eclipse is Lombok enabled by going to About Eclipse section and check for
"Lombok v1.18.4 "Envious Ferret" is installed. https://projectlombok.org/"




Add @Getters, @Setters annotation on our POJO


Now let us rewrite our POJO with Lombok annotations : 
package com.blogspot.javasolutionsguide.model;

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

@Getter
@Setter
public class EmployeeWithLombok {
    private int id;
    private String firstName;
    private String lastName;
    private int age;
    private String department;
}



As you can see, We have added @Getter and @Setter annotations on top of our POJO class which will
make sure that Lombok will add setters and getters in the .class file.

Create a Test class to test setters and getters 

package com.blogspot.javasolutionsguide.lombokTest;

import com.blogspot.javasolutionsguide.model.EmployeeWithLombok;

public class TestEmployeeWithLombok {
    public static void main(String[] args) {
        EmployeeWithLombok employeeWithLombok = new EmployeeWithLombok();
        employeeWithLombok.setFirstName("Gaurav");
        employeeWithLombok.setLastName("Bhardwaj");
        System.out.println("Employee First Name:" + employeeWithLombok.getFirstName() + "\n" +
                "Employee Last name:" + employeeWithLombok.getLastName());
    }
}

Output :
Employee First Name:Gaurav
Employee Last name:Bhardwaj

Summary

So in this tutorial, we learnt how we can use Lombok library to generate setters and getters ,which results
into cleaner code.

Please feel free for any comments, Questions or to share it with someone you feel it might be helpful.

Also, if you like JavaSolutionsGuide and want to contribute, go ahead and

- Write an article
- Share with me at javasolutionsguide@gmail.com and
- See Your Article Getting Published on main Page of JavaSolutionsGuide with your name.