Your code nearly works. Take a close look at the method signature of Enum#values
(documentation).
It is no method that accepts an argument, it returns the whole array. So you need to shift the access after the method as array-access:
Season.values()[2]
You should avoid accessing Enum
by index. They depend on their order in the code. If you do some refactoring like "sort project alphabetical":
public enum Season {
FALL, // 3 -> 0
SPRING, // 1 -> 1
SUMMER, // 2 -> 2
WINTER // 0 -> 3
}
or later add some values to your enum without remembering your order-depend code, then you will break your code. Instead you may setup a Map<Integer, Season>
:
Map<Integer, Season> indexToSeason = new HashMap<>();
indexToSeason.put(0, Season.WINTER);
indexToSeason.put(1, Season.SPRING);
indexToSeason.put(2, Season.SUMMER);
indexToSeason.put(3, Season.FALL);
And then use that map for access:
public Season getSeasonByIndex(int index) {
return indexToSeason.get(index);
}