Understanding Enum using SWITCH CASE

CODE


enum Level { //use uppercase for first letter
    
    LOW,MEDIUM,HIGH;
}
class apples{
    
    public static void main (String args[]) {
       
        Level myvar = Level.HIGH; //learn this syntax to access ENUM constants
       
        switch (myvar) {
        case LOW :
            System.out.println("Low Level");
            break;
           
        case MEDIUM:   
            System.out.println("Medium Level");
            break;
       
        case HIGH:
            System.out.println("High Level");
            break;
        }
       
        System.out.println(myvar); //this is the way to print constant of ENUM
    }
}


OUTPUT

REFERENCE

  • https://www.w3schools.com/java/java_enums.asp
  • https://www.youtube.com/watch?v=LYKHxwQ0QH8


Comments

Popular posts from this blog

Finding index of an Array Element

Array Elements as Counters

Question Practice on Random Number and Loops