Overriding class level Getter and Setter annotations with method level Getter and Setter annotations

In this post under Lombok, I will show how we can override Getter and Setter annotations (applied at class level) default settings by appliying same annotations at method level.

Below is the example of pojo class for your reference.

1  package package3;
3  import java.util.Date;
5  import lombok.AccessLevel;
6  import lombok.Getter;
7  import lombok.Setter;
9  @Getter
10 @Setter
11 public class Person {
12     private int id;
13     private String firstName;
14     private String middleName;
15     private String lastName;
16     private int age;
17     @Getter(AccessLevel.NONE)
18     private Date dateOfBirth;
19 }

In the above pojo class, at line 9 and 10, we are adding Getter and Setter annotation at class level instructing the Lombok to generate public getter and setter methods.

At line 17, we are again adding Getter annotation to “dateOfBirth” field specifically with “AccessLevel.NONE” attribute.

“AccessLevel.NONE” indicates Lombok to not generate getter method for this particular field overriding the default settings configured at class level.

In this way, we can override the settings applied at class level with the settings at method level.

Leave a Reply