Centurion, South Africa info@code2bits.com On Request

How to Use the @Getter and @Setter Annotations of Project Lombok

This post will focus on how to make use of the @Getter and @Setter annotations of Project Lombok. The Lombok Project is a java library that helps a developer generate boilerplate code. By simply adding the Lombok library to your IDE and build path the Lombok library will auto-generate the Java bytecode, as per the annotations, into the .class files.

The @Getter and @Setter annotations are added to a class or member attributes and Lombok will generate the implementation of the getter and setter methods.

Getting Started

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 Project Lombok to generate boilerplate code in java projects.

Java Mutators and Accessors (Getters & Setters)

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 Object Oriented principles to the design of your Java class and require the getter and setter methods.

If a Java class contains a large number of member 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 fewer errors can occur when writing the code yourself, and the class is easier to read and understand.

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.

  • The public access modifier specifies that the member and/or method can be accessed from anywhere.
  • The protected 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.
  • The default access modifier (package-private or no modifier specified) specifies that the member and/or method is only visible within its own package.
  • The private access modifier specifies that the member and/or method can only be accessed in its own class.

Code Example

The code example can be downloaded from Github from the button below.

Download Code

Define Maven Dependencies

The following dependencies should be included in the pom.xml file.

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.8</version>
  <scope>provided</scope>
</dependency>

Example 1: @Getter and @Setter on Class

This example 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. This is similar as if you annotate all the non-static fields in that class with the annotations.

Original Class

@Getter
@Setter
public class GetterSetterExample01 {
    private long id;
    private String firstName;
    private String lastName;
    private String gender;
    private Date birthDate;
    private String contactEmail;
    private boolean isFirstYear;
}

To truly appreciate the magic of the Lombok library, you should compile the GetterSetterExample01 class by making use of maven to build your project. As part of the compile process, Lombok will generate the boilerplate code depending on the type of annotation you used. To see the boilerplate code, you should decompile GetterSetterExample01.class file. An easy way to do this is to make use of the following URL:

http://www.javadecompilers.com

Decompiled Class

public class GetterSetterExample01
{
    private long id;
    private String firstName;
    private String lastName;
    private String gender;
    private Date birthDate;
    private String contactEmail;
    private boolean isFirstYear;
    
    public long getId() {
        return this.id;
    }
    
    public String getFirstName() {
        return this.firstName;
    }
    
    public String getLastName() {
        return this.lastName;
    }
    
    public String getGender() {
        return this.gender;
    }
    
    public Date getBirthDate() {
        return this.birthDate;
    }
    
    public String getContactEmail() {
        return this.contactEmail;
    }
    
    public boolean isFirstYear() {
        return this.isFirstYear;
    }
    
    public void setId(final long id) {
        this.id = id;
    }
    
    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }
    
    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }
    
    public void setGender(final String gender) {
        this.gender = gender;
    }
    
    public void setBirthDate(final Date birthDate) {
        this.birthDate = birthDate;
    }
    
    public void setContactEmail(final String contactEmail) {
        this.contactEmail = contactEmail;
    }
    
    public void setFirstYear(final boolean isFirstYear) {
        this.isFirstYear = isFirstYear;
    }
}

Example 2: @Getter and @Setter on member attributes

This example 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 example will also describe how to define Access Levels for the mutator and accessor methods.

Original Class

public class GetterSetterExample02 {

    @Getter @Setter(AccessLevel.PRIVATE)
    private long id;

    @Getter @Setter(AccessLevel.PROTECTED)
    private String firstName;

    @Getter @Setter(AccessLevel.PUBLIC)
    private String lastName;

    @Getter @Setter(AccessLevel.PACKAGE)
    private String gender;

    @Getter @Setter(AccessLevel.MODULE)
    private Date birthDate;

    @Getter @Setter(AccessLevel.NONE)
    private String contactEmail;

    @Getter @Setter
    private boolean isFirstYear;
}

To see the boilerplate code, you should decompile GetterSetterExample02.class file. An easy way to do this is to make use of the following URL:

http://www.javadecompilers.com

Decompiled Class

public class GetterSetterExample02
{
    private long id;
    private String firstName;
    private String lastName;
    private String gender;
    private Date birthDate;
    private String contactEmail;
    private boolean isFirstYear;
    
    public long getId() {
        return this.id;
    }
    
    private void setId(final long id) {
        this.id = id;
    }
    
    public String getFirstName() {
        return this.firstName;
    }
    
    protected void setFirstName(final String firstName) {
        this.firstName = firstName;
    }
    
    public String getLastName() {
        return this.lastName;
    }
    
    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }
    
    public String getGender() {
        return this.gender;
    }
    
    void setGender(final String gender) {
        this.gender = gender;
    }
    
    public Date getBirthDate() {
        return this.birthDate;
    }
    
    void setBirthDate(final Date birthDate) {
        this.birthDate = birthDate;
    }
    
    public String getContactEmail() {
        return this.contactEmail;
    }
    
    public boolean isFirstYear() {
        return this.isFirstYear;
    }
    
    public void setFirstYear(final boolean isFirstYear) {
        this.isFirstYear = isFirstYear;
    }
}

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

  • Member Attribute: id - The accessor method is public because the AccessLevel was not specified. The mutator method is private because the AccessLevel was set to PRIVATE.
  • Member Attribute: firstName - The accessor method is public because the AccessLevel was not specified. The mutator method is protected because the AccessLevel was set to PROTECTED.
  • Member Attribute: lastName - The accessor method is public because the AccessLevel was not specified. The mutator method is public because the AccessLevel was set to PUBLIC.
  • Member Attribute: gender - The accessor method is public because the AccessLevel was not specified. The mutator method is "default" because the AccessLevel was set to PACKAGE.
  • Member Attribute: birthDate - The accessor method is public because the AccessLevel was not specified. The mutator method is "default" because the AccessLevel was set to MODULE.
  • Member Attribute: contactEmail - The accessor method is public because the AccessLevel was not specified. The mutator method is not generated because the AccessLevel was set to NONE.
  • Member Attribute: isFirstYear - The accessor method is public because the AccessLevel was not specified. The accessor method is public because the AccessLevel was not specified.

Summary

Congratulations !!! You have successfully used the @Getter and @Setter annotations of Project Lombok. Please look out for more examples on how to make use of Project Lombok to simplify your Java coding experience.