Wednesday, April 15, 2020

Java enums

Let us see another example of Java enum where we are using value(), valueOf(), and ordinal() methods of Java enum.
  1. class EnumExample1{  
  2. //defining enum within class  
  3. public enum Season { WINTER, SPRING, SUMMER, FALL }  
  4. //creating the main method  
  5. public static void main(String[] args) {  
  6. //printing all enum  
  7. for (Season s : Season.values()){  
  8. System.out.println(s);  
  9. }  
  10. System.out.println("Value of WINTER is: "+Season.valueOf("WINTER"));  
  11. System.out.println("Index of WINTER is: "+Season.valueOf("WINTER").ordinal());  
  12. System.out.println("Index of SUMMER is: "+Season.valueOf("SUMMER").ordinal());  
  13.   
  14. }}  
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

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...