mengmeng
mengmeng

Reputation: 1496

Lombok won't generate getter methods for variables when annotation is in class level

I'm quite confused as to why the annotation won't create getter methods for the variables if the annotation is at class level

@Getter
public class Config {

    private static final String TEST = "";
}

But creates the getters if annotation is at the variable level.


public class Config {
    @Getter
    private static final String TEST = "";
}

Thank you!

Upvotes: 3

Views: 2881

Answers (2)

DPWork
DPWork

Reputation: 488

You can do this as the documentation says:

You can annotate a class with a @Getter or @Setter annotation. Doing so is equivalent to annotating all non-static fields in that class with that annotation. @Getter/@Setter annotations on fields take precedence over the ones on classes.

The problem you're seeing is because the field you're having problems with is static.

Upvotes: 2

Abhishek
Abhishek

Reputation: 960

The problem is Class level Getter annotation doesn't work with Static fields. Check the Lombok documentation here. Please refer to the screenshot below --

enter image description here

Upvotes: 1

Related Questions