Let us see another example of Java enum where we are using value(), valueOf(), and ordinal() methods of Java enum.
- class EnumExample1{
-
- public enum Season { WINTER, SPRING, SUMMER, FALL }
-
- public static void main(String[] args) {
-
- for (Season s : Season.values()){
- System.out.println(s);
- }
- System.out.println("Value of WINTER is: "+Season.valueOf("WINTER"));
- System.out.println("Index of WINTER is: "+Season.valueOf("WINTER").ordinal());
- System.out.println("Index of SUMMER is: "+Season.valueOf("SUMMER").ordinal());
-
- }}
Output:
WINTER
SPRING
SUMMER
FALL
Value of WINTER is: WINTER
Index of WINTER is: 0
Index of SUMMER is: 2
source:
https://www.javatpoint.com/enum-in-java
No comments:
Post a Comment