DEV Community

André Maré
André Maré

Posted on • Updated on

How to generate Java Getters and Setters with Lombok (Part 1)

Quick Summary

The Lombok Project is a java library that helps a developer generate boilerplate code like "getter" and "setter" methods for Plain Old Java Objects (POJOs). By simply adding the Lombok library to your IDE and build path, the Lombok library will autogenerate the Java bytecode, as per the annotations, into the .class files.

This post will focus on how to make use of the @Getter and @setter annotations on the top off a class to generate the mutator and accessor methods of a class.

Requirements

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

This will form part of a multi-part series on how to use the Lombok java library to auto generate the getter and setter methods for Java classes.

Introduction

In Java, a mutator method is a method used to control changes to a variable within a Java class. They are also widely known as setter methods. Often a setter is accompanied by a getter (also known as an accessor), which returns the value of the private member variable.

There are many opinions about the use of Getter and Setter methods, and when to use them correctly, but this will not be discussed in this post. Let's assume that you have correctly applied OO principles to the design of your Java class and require the getter and setter methods.

If a Java class contains a large number of members attributes, it will contain a large number of boilerplate code like the getter and setter methods, and hence the use of the Lombok library makes it much easier to generate the code. This means less errors can occur when writing the code yourself, and the class is easier to read and understand.

Example 1: Getter & Setter on Class

The @Getter and/or @setter annotation can be placed on the class declaration. This is similar as if you annotate all the non-static fields in that class with the annotations.

The following example illustrates how to add the @Getter and/or @setter annotation to the top of the class.

Student_Code

To truly appreciate the magic of the Lombok library, you should compile the Student class by making use of the "javac" command within a terminal. To run the command successfully, you should ensure that the Lombok Jar file and the Student.java file are within the same directory from where you execute the command. After the Student class has been compiled, you should disassemble the Student Java class file by making use of the "javap" command.

$ javac -cp lombok.jar Student.java
$ javap Student.class

The javap command prints out the package, protected, and public fields and methods of the class passed to it. The javap command displays its output to stdout. Therefor the private member attributes of the Student class is not printed out, but one can now see how a getter and setter method has been created for each of the non-static private members of the class.

Student_Code_Decompile

The 7 getter (accessor) methods are listed from line 3 - 9, and the 7 setter (mutator) methods are listed from line 10 - 16. The generated getter/setter methods will be public, unless you explicitly specify an AccessLevel. Access levels for getter and setter annotations will be discussed in the next post.

Summary

Congratulations !!! You have successfully generated Getter and Setter methods for a Java class by making use of the Lombok java library. Please lookout for more examples on how to make use of Project Lombok to simplify your Java coding experience.

Top comments (5)

Collapse
 
marirsri profile image
Sriram

how do i set the value for the setter methods for the property if i use the lomok? I have a class as below:

@Getter
@Setter
public class LombokDTO {
private int id;
}
I have created a object for this class to set value for id.
LombokDTO lto = new LombokDTO();
but i could not find setId method if i give lto. ?

Collapse
 
kevashan_g22 profile image
Stormzy's indian cousin

Lombok makes your classes so much neater and more readable, dumbfounded that more people dont use it. Thanks for the tutorial

Collapse
 
andremare profile image
André Maré

I agree. Not only are they more readable and neater, but the chances of errors creeping in are also less in classes where there are a large number of member attributes.

Watch this space, I am in the process of creating more of these tutorials.

Collapse
 
pnaranja profile image
Paul

Thanks for the intro! I use Lombok at work for our Automation Framework.

Just FYI - Lombok doesn't work with JDK 10 yet.

Collapse
 
andremare profile image
André Maré

Hi Paul, Thanks for the feedback. I will change the requirements section to indicate it as such.