How to generate Java Getters and Setters with Lombok (Part 2)
Tags: java, lombok, tutorial, beginners
Published: 2018-05-07 05:29:40

---
title: How to generate Java Getters and Setters with Lombok (Part 2)
published: true
description: This post will focus on how to make use of the @Getter and @Setter annotations on top off the member attributes of a class to generate the mutator and accessor methods for those fields. This post will also describe how to define Access Levels for the mutator and accessor methods.
tags: java, lombok, tutorial, beginners
---

<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 top off the member attributes of a class to generate the mutator and accessor methods for those fields. This post will also describe how to define Access Levels for the mutator and accessor methods.

<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>
You can annotate any field with @Getter and/or @Setter and the Lombok library will generate the access & mutator method for that specific member attribute. This allow you to be more specific for which fields you require accessor or mutator methods and even have the ability to specify the Java Access Modifiers for the particular methods.

Java Access Modifiers specifies the scope (accessibility) of member attributes, methods, constructors and classes. There are 4 types of access modifiers in Java namely, private, default, protected and public.

<ul>
<li>The <strong>public</strong> access modifier specifies that the member and/or method can be accessed from anywhere.</li>
<li>The <strong>protected</strong> access modifier specifies that the member and/or method  can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.</li>
<li>The <strong>default</strong> access modifier (package-private or no modifier specified) specifies that the member and/or method  is only visible within its own package.</li>
<li>The <strong>private</strong> access modifier specifies that the member and/or method can only be accessed in its own class.</li>
</ul>

<h3>Example</h3>
<h4>Getter & Setter on Fields with AccessLevels</h4>
It is possible to specify the access level for the @Getter and/or @Setter annotations to auto generate the accessor and mutator methods with the appropriate Java Access Modifiers. This allows you to control the accessibility of the methods via the Lombok library.

The generated accessor/mutator method will be public unless you explicitly specify an AccessLevel as part of the annotation. The example below illustrates the use AccessLevels to specify the Java Access Modifier for each method.

You can always manually disable getter/setter generation for any field by using the special AccessLevel.NONE access level.

![Student_Code](https://thepracticaldev.s3.amazonaws.com/i/yc2a2j2wr7e3je7dse80.png)

The following table illustrates how the Lombok AccessLevel will be mapped to the Java Access Modifier to be used for the access and mutator methods.

<table>
<tr><th align="left">Lombok AccessLevel</th><th align="left">Java Access Modifier</th></tr>
<tr><td>AccessLevel.PRIVATE</td><td>private</td></tr>
<tr><td>AccessLevel.PROTECTED </td><td>protected</td></tr>
<tr><td>AccessLevel.PUBLIC </td><td>public</td></tr>
<tr><td>AccessLevel.PACKAGE </td><td>default</td></tr>
<tr><td>AccessLevel.MODULE</td><td>default</td></tr>
<tr><td>AccessLevel.NONE</td><td>Method Not Generated !!!</td></tr>
<tr><td>Not Specified</td><td>public</td></tr>
</table>

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, depending on the AccessLevel that was set as part of the annotation.

![Student_Decompile](https://thepracticaldev.s3.amazonaws.com/i/13pr9n64tdkrv4un3vta.png)

So based on the example, the following are the reasons why lombok generated the methods with different java access modifiers:

<strong>Member Attribute: id</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is private because the AccessLevel was set to PRIVATE. 

<strong>Member Attribute: firstName</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is protected because the AccessLevel was set to PROTECTED. (See line 5)

<strong>Member Attribute: lastName</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is public because the AccessLevel was set to PUBLIC. (See line 7)

<strong>Member Attribute: gender</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is "default" because the AccessLevel was set to PACKAGE. (See line 9)

<strong>Member Attribute: birthDate</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is "default" because the AccessLevel was set to MODULE. (See line 11)

<strong>Member Attribute: contactEmail</strong>
The accessor method is public because the AccessLevel was not specified. The mutator method is not generated because the AccessLevel was set to NONE.

<strong>Member Attribute: isFirstYear</strong>
The accessor method is public because the AccessLevel was not specified. The accessor method is public because the AccessLevel was not specified.

<h3>Summary</h3>
This post has shown another example of how to make use of the Lombok library together with the @Getter and @Setter annotations to generate the methods. It has further shown how to specify the expected java access modifier for each of generated methods. The next post in the series will illustrate how to make use of lombok config to change how the methods are generated.