Reputation: 91
Code:
enum EBtnSts
{
static
{
ePlayBtn = new EBtnSts("ePlayBtn", 1);
EBtnSts[] arrayOfEBtnSts = new EBtnSts[0];
arrayOfEBtnSts[0] = ePlayBtn;
}
}
Upvotes: 0
Views: 146
Reputation: 213223
That is a crazy use of a static initializer
, which you should really avoid. For one, that will certainly throw an ArrayIndexOutOfBounds
exception.
EBtnSts[] arrayOfEBtnSts = new EBtnSts[0]; // Creates an array of length 0
arrayOfEBtnSts[0] = ePlayBtn; // You can't access any index of 0 length array.
Secondly, that code is implementing an enum
as a normal class. Avoid. The variable ePlayBtn
should be an enum constant. There should be 2 fields in the enum
containing the value which you are passing in constructor. And don't invoke the constructor like that.
Also, the creation of array is totally non-sense. You can directly get the array of enum constants using values()
method of your enum
.
The enum
is better implemented as:
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();
}
You can learn more about enum types here.
Upvotes: 5