java-枚举静态数组和注释值的混淆
作者:互联网
我有枚举:
public enum Scope {
USER, GLOBAL;
public static final Scope[] TRANSIENT = {};
public static final Scope[] USER_OVER_GLOBAL = {GLOBAL, USER};
public static final Scope[] GLOBAL_OVER_USER = {USER, GLOBAL};
}
和注释:
public @interface Config {
public Scope[] load() default Scope.GLOBAL_OVER_USER; // Can't use this defval
public Scope[] save() default Scope.USER;
}
为什么我不能使用静态数组作为注释属性的默认值?我的NetBeans 7.3 Beta告诉我有必需的范围,但是找到了Scope []-如您所见,这是不正确的.是否存在与NB或Java 7相关的混乱?
解决方法:
问题是Scope [] GLOBAL_OVER_USER并非全部恒定. (是的,数组本身是常量,但是您可以更改其内容,例如GLOBAL_OVER_USER [0] = GLOBAL;.
一种解决方法是直接在注释声明中初始化数组:
public Scope[] load() default {USER, GLOBAL};
标签:enums,annotations,netbeans-7,java 来源: https://codeday.me/bug/20191031/1977368.html