Lombok不会在注解位于类级别时为变量生成getter方法。

huangapple go评论67阅读模式
英文:

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

问题

当注解位于类级别时,它不会为变量创建getter方法:

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

但如果将注解放在变量级别,它会创建getter方法:

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

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!

答案1

得分: 2

你可以按照文档所示来完成这个操作:

> 你可以为一个类添加@Getter或@Setter注解。这样做等同于为该类中的所有非静态字段添加该注解。字段上的@Getter/@Setter注解优先于类上的注解。

你所遇到的问题是因为你遇到的字段是static

英文:

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.

答案2

得分: 1

问题是Class级别的Getter注解在处理Static字段时不起作用。请查看Lombok文档这里。请参考下面的屏幕截图 --

Lombok不会在注解位于类级别时为变量生成getter方法。

英文:

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

Lombok不会在注解位于类级别时为变量生成getter方法。

huangapple
  • 本文由 发表于 2020年10月27日 19:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64553333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定