Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不能';我不能在构造函数中访问枚举中的静态字段吗?_Java - Fatal编程技术网

Java 为什么不能';我不能在构造函数中访问枚举中的静态字段吗?

Java 为什么不能';我不能在构造函数中访问枚举中的静态字段吗?,java,Java,可能重复: 我的情况: enum Attribute { POSITIVE, NEGATIVE } enum Content { C1(Attribute.POSITIVE), C2(Attribute.POSITIVE), ... // some other positive enum instances. Cm(Attribute.NEGATIVE), ... // some other negative enum instances. Cn

可能重复:

我的情况:

enum Attribute { POSITIVE, NEGATIVE }
enum Content {
    C1(Attribute.POSITIVE),
    C2(Attribute.POSITIVE),
    ... // some other positive enum instances.
    Cm(Attribute.NEGATIVE),
    ... // some other negative enum instances.
    Cn(Attribute.NEGATIVE);

    private final Atrribute a;
    static int negativeOffset = 0;
    private Content(Atrribute a) {
        this.a = a;
        if ( a.compareTo(Attribute.POSITIVE) == 0 ) {
               negativeOffset ++;
        }
    }

    public static int getNegativeOffset() { return negativeOffset; }
} 
我的意图是每当我添加一个新的枚举(带有正数属性)时,将negativeOffset加1,然后我可以调用getNegativeOffset()来获取negative的起点 枚举并做任何我想做的事

但科米勒抱怨说

无法引用静态枚举字段内容。初始值设定项中的negativeOffset

您可以使用以下“技巧”:

然后像这样引用变量:

IntHolder.negativeOffset ++;

这样做的原因是JVM保证在初始化
IntHolder
静态内部类时初始化变量,这在访问它之前不会发生

然后,整个类将编译如下:

enum Attribute { POSITIVE, NEGATIVE }
enum Content {
    C1(Attribute.POSITIVE),
    C2(Attribute.POSITIVE),
    ... // some other positive enum instances.
    Cm(Attribute.NEGATIVE),
    ... // some other negative enum instances.
    Cn(Attribute.NEGATIVE);

    private final Attribute a;

    private static class IntHolder {
        static int negativeOffset;
    }

    private Content(Attribute a) {
        this.a = a;
        if ( a == Attribute.POSITIVE) {
               IntHolder.negativeOffset ++;
        }
    }

    public static int getNegativeOffset() { return IntHolder.negativeOffset; }
}

请注意拼写错误的更正,以及使用
=
而不是
compareTo()

属性
枚举值进行简单比较。这将有所帮助。谢谢,技巧不错!我没想到这个线程安全技巧在这里也能起作用!顺便说一句,我仍然想知道为什么原来的方法不起作用。静态fileld初始化不是发生在构造函数之前吗?您的问题是枚举不能在实例之前有任何声明,这意味着您不能初始化(或使用)实例构造函数所需的任何静态字段。但是,通过将字段放在另一个类(尽管是内部静态类)中,另一个类会在第一次使用时加载(并静态初始化),这是在构造函数中。这种模式是一种非常好的方法,可以在不进行任何同步的情况下对单例进行线程安全惰性初始化。
return IntHolder.negativeOffset; 
enum Attribute { POSITIVE, NEGATIVE }
enum Content {
    C1(Attribute.POSITIVE),
    C2(Attribute.POSITIVE),
    ... // some other positive enum instances.
    Cm(Attribute.NEGATIVE),
    ... // some other negative enum instances.
    Cn(Attribute.NEGATIVE);

    private final Attribute a;

    private static class IntHolder {
        static int negativeOffset;
    }

    private Content(Attribute a) {
        this.a = a;
        if ( a == Attribute.POSITIVE) {
               IntHolder.negativeOffset ++;
        }
    }

    public static int getNegativeOffset() { return IntHolder.negativeOffset; }
}