Omit Lombok @Getter and @Setter

Learn to add default accessor methods (getter and setter) using Lombok and omit these methods for specific requirements.

1. Default Behavior

Lombok provides two annotations to add getter and setter methods to a POJO.

  • @Getter – can be applied on any field and class. When applied on a field, lombok build a standard getter for that field. When applied on a class, lombok builds getter methods for all non-static fields.
  • @Setter – works similar to @Getter, only difference is it make lombok to build standard setter methods for field or all non-static fields in a class.

For example, let us see the Tag class where we have added the @Getter and @Setter annotations. Lombok will build standard getters and setters for id and name fields.

@Getter
@Setter
class Tag {
      private long id;
      private String name;
}
Lombok Accessors
Lombok added these accessor methods

2. Omit Accessors on Fields

To prevent Lombok building accessor methods on fields, we have two choices:

2.1. Set AccessLevel.NONE on Class

Class level AccessLevel.NONE will make Lombok not build any accessor method for any field in the class. It is equivalent to not applying the accessor annotation at all.

In the below example, Lombok will build only getter methods and will skip generating all setter methods.

@Getter
@Setter(AccessLevel.NONE)
class Tag 
{
      private long id;
      private String name;
      private boolean status;
}

2.2. Set AccessLevel.NONE on Fields

To skip the setter method for only specific fields, we need to use AccessLevel.NONE on those fields only.

There will not be any setter method for field status in the given example.

@Getter
@Setter
class Tag 
{
      private long id;
      private String name;
      
      @Setter(AccessLevel.NONE)
      private boolean status;
}

3. @Data Annotation

Lombok @Data annotation is a shortcut for applying a group of annotations in a single statement. These annotations are:

  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @ToString
  • @EqualsAndHashCode

Clearly, @Data causes the class to have public getter and setter methods. If we want to omit the default accessors on a few fields, we can apply AccessLevel.NONE on the class fields as shown in the previous section.

In the given example, Lombok will not build the setter method for field status.

@Data
class Tag 
{
      private long id;
      private String name;
      
      @Setter(AccessLevel.NONE)
      private boolean status;
}

4. Conclusion

It is clear from the above discussion that to omit the default generated getter and setter methods by Lombok, we must use the AccessLevel.NONE along with respective @Getter or @Setter annotations.

When AccessLevel.NONE is applied on class, it prevents Lombok from building accessors for all fields. When used on a field, the effect is limited to that field only.

Happy Learning !!

Sourceocde Download

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial