我是java枚举的新手,我对为什么此代码可以正常编译感到困惑

enum Scale5 {
GOOD(), BETTER(), BEST();
static  Scale5 s=GOOD;
}

但是此代码失败:
enum Scale5 {
GOOD(), BETTER(), BEST();
Scale5 s=GOOD;
}

我得到的错误是:从初始化程序非法引用静态字段。
我不明白原因,我在枚举方面相对缺乏经验,所以请帮我丢下它。非常感谢!

在这里Cannot refer to the static enum field within an initializer?提出的问题与我所问的完全相反。在我的情况下,将s声明为static可以编译代码。

最佳答案

想像这样的枚举:

public final class Scale5
{
     public static final Scale5 GOOD = new Scale5();
     public static final Scale5 BETTER = new Scale5();
     public static final Scale5 BEST = new Scale5();

     static Scale5 s = GOOD;//works because GOOD is initialized first;
     Scale5 ss = GOOD;//doesn't work because in order to initialize GOOD,
                      //ss must be assigned an object that is not yet initialized;
}

07-27 13:50