Java Enum access to private instance variable

后端 未结 6 1836
名媛妹妹
名媛妹妹 2021-02-05 10:50

Consider the example:

enum SomeEnum {
    VALUE1(\"value1\"),
    VALUE2(\"value2\"),
    VALUE3(\"value3\")
    ;
    private String value;

    private SomeEnu         


        
6条回答
  •  情深已故
    2021-02-05 11:42

    The enums itself (VALUE1, VALUE2 and VALUE3) are static and final. They are three instances of the SomeEnum type and we can't change them (like deleting VALUE1 from the enum or adding a VALUE4 at runtime).

    But we can add public fields to the instances (like in your example) and alter the content of those fields at runtime.

提交回复
热议问题