这是一个疯狂的使用static initializer
,你应该真的避免.首先,这肯定会引发ArrayIndexOutOfBounds
异常.
EBtnSts[] arrayOfEBtnSts = new EBtnSts[0]; // Creates an array of length 0
arrayOfEBtnSts[0] = ePlayBtn; // You can't access any index of 0 length array.
Run Code Online (Sandbox Code Playgroud)
其次,该代码实现了enum
一个普通的类.避免.变量ePlayBtn
应该是枚举常量.在enum
包含您在构造函数中传递的值中应该有2个字段.并且不要像这样调用构造函数.
此外,阵列的创建完全没有意义.您可以使用values()
您的方法直接获取枚举常量数组enum
.
将enum
被更好地实现为:
enum EBtnSts {
E_PLAY_BTN("ePlayBtn", 1);
private final String value;
private final int id;
private EBtnSts(String value, int id) {
this.value = value;
this.id = id;
}
private final EBtnSts[] VALUES = values();
}
Run Code Online (Sandbox Code Playgroud)