class中定义的枚举是静态嵌套的class?

Enums defined in a class is a static nested class?

对于class中定义的枚举,如

class OuterClass {
    public enum Method {
        GET,
        PUT,
        POST,
        DELETE;
    }
}

枚举是否为静态嵌套class (https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)?从用于引用它的语法来看,情况似乎是这样。还是非静态嵌套class(内部class)?

enum声明生成的字节码如下:

// compiled from: OuterClass.java
public final static enum INNERCLASS ...

所以是的,在这种情况下,enum 是一个 静态嵌套 class - 在 JLS.

中确认

根据§8.9 of the JLS

An enum declaration specifies a new enum type, a special kind of class type.

[...]

A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier. [...]

The JLS says

An enum declaration specifies a new enum type, a special kind of class type.

所以看起来 Oracle 的说法是枚举是 classes。

如果您在另一个 class 中声明一个枚举,那么是的,它是一个内部 class。枚举总是静态的,所以 ,当它在另一个 [=20] 中声明时,将枚举称为静态内部 class(或嵌套 class)是公平的=].