Creating a final Java class array of enum constants with values( )

后端 未结 2 888
故里飘歌
故里飘歌 2021-01-21 07:54

Inside a Java enumerated class, I\'d like to create a final static array containing the values() of the class. When I do this along the following lines

2条回答
  •  旧时难觅i
    2021-01-21 07:59

    Can you provide an example where this happens because it shouldn't be null.

    public class Main {
        public enum Name {
            E1(  ), E2(  );
            private static final Name[] VALUES = Name.values();
        }
    
    
        public static void main(String... args) {
            System.out.println(Name.VALUES);
            System.out.println(Arrays.asList(Name.VALUES));
        }
    }
    

    prints

    [LMain$Name;@717e5fde
    [E1, E2]
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题