Core Java

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 :

– Create a POJO without Lombok and add setters getters manually

– Create POJO and use Lombok library to add getters setters

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;
/**
 * @author JavaSolutionsGuide
 *
 */
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;
/**
 * @author JavaSolutionsGuide
 *
 */
public class TestEmployeeWithoutLombok {

 /**
  * 
  * @param args
  * 
  */
 public static void main(String[] args) {
  //Test EmployeeWithoutLombok
  
  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.

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). 

  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;


/**
 * @author JavaSolutionsGuide
 *
 */
@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;


/**
 * Main class to test Lombok.
 *
 */
public class TestEmployeeWithLombok 
{
    public static void main( String[] args ) {
     //Test EmployeeWithLombok
     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.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to Use Lombok to remove boilerplate setters getters in Java

Opinions expressed by Java Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button