How to generate Java Getters and Setters with Lombok (Part 1)
Tags: java, lombok, tutorial, beginners
Published: 2018-05-03 08:02:38

---
title: How to generate Java Getters and Setters with Lombok (Part 1)
published: true
description: 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.
tags: Java, Lombok, Tutorial, Beginner
---

<h3>Quick Summary</h3>
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.

<h3>Requirements</h3>
The following list defines the technologies and libraries I used to implement the sample code:
<ul>
<li>Lombok Library V 1.16.20. <a href="https://projectlombok.org/download">(Download)</a></li>
<li><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">JDK 1.8</a></li>
<li>Spring Tool Suite <a href="https://spring.io/tools/sts/all">(Download)</a> </li>
</ul>

<h3>Multiple Posts</h3>
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.
<ul>
<li><a href="https://www.code2bits.com/how-to-generate-java-getters-and-setters-with-lombok-part-1/">How to generate Java Getters and Setter with Lombok (Part 1)</a></li>
<li><a href="https://www.code2bits.com/how-to-generate-java-getters-and-setters-with-lombok-part-2/">How to generate Java Getters and Setter with Lombok (Part 2)</a></li>
</ul>

<h3>Introduction</h3>
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.

<h3>Example 1: Getter & Setter on Class</h3>
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](https://thepracticaldev.s3.amazonaws.com/i/hxoqbxdsf0uuw01kbnpf.png)

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.

<pre>
$ javac -cp lombok.jar Student.java
$ javap Student.class
</pre>

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](https://thepracticaldev.s3.amazonaws.com/i/s1hjk3c048jzhk7k6gsu.png)

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. 

<h3>Summary</h3>
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.