public class Test {

public enum Directions {
        NORTH, WEST, SOUTH, EAST
    }

    static final Directions D1 = Directions.NORTH;

    static class Inner {
        static final Directions D2 = Directions.NORTH;
    }

    class Inner2 {
        static final Directions D3 = Directions.NORTH;
    }

}

我得到标题中的IDE-Error,引用变量D3。有人可以向我解释吗?为什么我不能在不是静态的内部类中声明静态变量,为什么枚举值不是常量?

最佳答案

JLS §8.1.3 Inner Classes and Enclosing Instances



Why is an Enum entry not considered a constant variable?

关于java - 除非使用常量表达式初始化,否则不能在非静态内部类型中将字段声明为静态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37509919/

10-12 00:34