枚举静态数组和注释值混淆

时间:2012-11-13 11:43:33

标签: java enums annotations netbeans-7

我有枚举:

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但找到Scope[] - 因为您可以看到这不是真的。是否存在NB或Java 7相关的混淆?

1 个答案:

答案 0 :(得分:4)

问题在于Scope[] GLOBAL_OVER_USER并非全部不变。 (是的,数组本身是常量,但您可以更改它的内容,例如GLOBAL_OVER_USER[0] = GLOBAL;

解决方法是直接在注释声明中启动数组:

public Scope[] load() default {USER, GLOBAL};