How to get Enum Value from index in Java?

jk_ picture jk_ · Jul 14, 2011 · Viewed 144.5k times · Source

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?

Answer

Harry Joy picture Harry Joy · Jul 14, 2011

Try this

Months.values()[index]