javaenums

How to get Enum Value from index in Java?


I have an enum in Java:

public enum Months
{
    JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
}

I want to access enum values by index, e.g.

Months(1) = JAN;
Months(2) = FEB;
...

How shall I do that?


Solution

  • Try this

    Months.values()[index]