0 votes
1 view
in Java by (4.1k points)

What is the correct way to cast an Int to an enum in Java given the following enum?

public enum MyEnum

{

    EnumValue1,

    EnumValue2

}

MyEnum enumValue = (MyEnum) x; //Doesn't work???

1 Answer

0 votes
by (46.1k points)
Use MyEnum.values()[x] where x requirement be 0 or 1, i.e. a legitimate ordinal for that enum.

Perceive that in Java enum are classes (and enum states thus are objects) and therefore you can't pick an int or even Integer to an enum.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...