Adding "private" to enum field changes static context

This question already has an answer here:

  • How to use fields in java enum by overriding the method? [duplicate] 8 answers

  • Try this

         ((TestEnum)this).x
    

    The error message is a misleading. The problem is, private field is not inherited, therefore x or this.x won't compile. You have to up-cast it to the superclass to access it.

    see also https://groups.google.com/d/msg/java-lang-fans/KVmmgeSCHkA/Kr_KxaaeolIJ

    The 'protected' access is probably better here

        protected int x;
    
    链接地址: http://www.djcxy.com/p/38074.html

    上一篇: 在构造函数之外访问枚举参数?

    下一篇: 将“private”添加到枚举字段会更改静态上下文