How do I define access modifiers in Lombok’s @Getter and @Setter annotations?

By default, when we use the Lombok’s @Getter and @Setter annotations the getters and setters will be created with public access modifier. We can however change the access modifier by setting the AccessLevel of the @Getter and @Setter annotations. The available choices for the access level are AccessLevel.PUBLIC, AccessLevel.PROTECTED, AccessLevel.PACKAGE, AccessLevel.PRIVATE. These enum values correspond to Java’s access modifier. While the AccessLevel.NONE will disable the getter and setter method generation.


import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class Person {
    private String firstName;

    private String lastName;
    private String gender;

    private int age;

How we use the Person class show in the snippet below:

package org.kodejava.lombok;


public class PersonDemo {
    public static void main(String[] args) {
        Person person = new Person();


If we try to see the generated class of the Person class we can run the following command to disassemble the class.

javap -p -cp .

And we got the following output of the javap command. As we can see that the setFirstName() method have a protected access modifier and the getAge() method have a private access modifier. The other mutator and accessor method all set to public access modifier.

public class {
    private java.lang.String firstName;
    private java.lang.String lastName;
    private java.lang.String gender;
    private int age;
    public java.lang.String getFirstName();
    public java.lang.String getLastName();
    public java.lang.String getGender();
    public void setLastName(java.lang.String);
    public void setGender(java.lang.String);
    public void setAge(int);
    protected void setFirstName(java.lang.String);
    private int getAge();

Maven Dependencies


Maven Central


Leave a Reply

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