DerMike
DerMike

Reputation: 16190

Omitting one Setter/Getter in Lombok

I want to use a data class in Lombok. Since it has about a dozen fields, I annotated it with @Data in order to generate all the setters and getters. However, there is one special field for which I don't want the accessors to be implemented.

How do I make Lombok omit this field?

Upvotes: 414

Views: 224259

Answers (2)

Mark Rotteveel
Mark Rotteveel

Reputation: 108965

According to @Data description you can use:

All generated getters and setters will be public. To override the access level, annotate the field or class with an explicit @Setter and/or @Getter annotation. You can also use this annotation (by combining it with AccessLevel.NONE) to suppress generating a getter and/or setter altogether.

Upvotes: 59

Michael Piefel
Michael Piefel

Reputation: 19968

You can pass an access level to the @Getter and @Setter annotations. This is useful to make getters or setters protected or private. It can also be used to override the default.

With @Data, you have public access to the accessors by default. You can now use the special access level NONE to completely omit the accessor, like this:

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private int mySecret;

Upvotes: 731

Related Questions