java get enum from number code example
Example 1: enum with numbers java
public enum EXIT_CODE {
A(104), B(203);
private int numVal;
EXIT_CODE(int numVal) {
this.numVal = numVal;
}
public int getNumVal() {
return numVal;
}
}
Example 2: get random number from enum in java
return Letter.values()[new Random().nextInt(Letter.values().length)];
Example 3: java enum from int
MyEnum.values()[x]
public enum MyEnum {
EnumValue1,
EnumValue2;
public static MyEnum fromInteger(int x) {
switch(x) {
case 0:
return EnumValue1;
case 1:
return EnumValue2;
}
return null;
}
}