Omitting one Setter/Getter in Lombok

JavaLombok

Java Problem Overview


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 getter. However there is one special field for which I don't want to the accessors to be implemented.

How does Lombok omit this field?

Java Solutions


Solution 1 - Java

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;

Solution 2 - Java

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.

Solution 3 - Java

Use the below code for omit/excludes from creating setter and getter. value key should use inside @Getter and @Setter.

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
private String mySecret;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerMikeView Question on Stackoverflow
Solution 1 - JavaMichael PiefelView Answer on Stackoverflow
Solution 2 - JavaMark RotteveelView Answer on Stackoverflow
Solution 3 - JavaSathiamoorthyView Answer on Stackoverflow