Skip to content
Related Articles

Related Articles

Usage of Enum and Switch keyword in Java
  • Last Updated : 11 Nov, 2020

An Enum is a unique type of data type in java which is generally a collection (set) of constants. More specifically, a Java Enum type is a unique kind of Java class. An Enum can hold constants, methods, etc. An Enum keyword can be used with if statement, switch statement, and iteration, etc. 

Example:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to show working of Enum 
// Keyword when declared outside the Class
enum Days {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
}
  
public class temp {
  
    // Driver method
    public static void main(String[] args)
    {
        Days x = Days.FRIDAY;
        System.out.println(x);
    }
}

chevron_right


Output:

FRIDAY

Switch Keyword

Switch statement is helpful when a user has a number of choices and wants to perform a different task for each choice. The Switch statement allows the testing of a variable for equality against a list of values. Each value is known as a case. A switch Case statement is generally used with a break statement but it is optional. 

Example:



Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to show working
// of Switch statement
public class Example_Switch {
  
    public static void main(String[] args)
    {
        // Declare a variable for switch statement
        int num = 50;
  
        // Switch keyword
        switch (num) {
        // Case statements
        case 10:
            System.out.println("10");
            break;
        case 20:
            System.out.println("20");
            break;
        case 30:
            System.out.println("30");
            break;
        // Default case statement
        default:
            System.out.println("Other than 10, 20 or 30");
        }
    }
}

chevron_right


Output:

Other than 10, 20 or 30

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive. Below are some examples to show working of Enum with Switch statement.

Example 1: Use of Enum with Switch statement when Enum is outside the main class

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to show the
// working of Enum keyword
// with Switch statement
  
// Enum keyword declared outside main class
enum Cars {
    BMW,
    JEEP,
    AUDI,
    VOLKSWAGON,
    NANO,
    FIAT;
}
  
// Main class
public class Main {
    public static void main(String args[])
    {
        // Declaring Enum variable
        Cars c;
        c = Cars.AUDI;
        // Switch keyword
        switch (c) {
  
        // Case statements
        case BMW:
            System.out.println("You choose BMW !");
            break;
        case JEEP:
            System.out.println("You choose JEEP !");
            break;
        case AUDI:
            System.out.println("You choose AUDI !");
            break;
        case VOLKSWAGON:
            System.out.println("You choose VOLKSWAGON !");
            break;
        case NANO:
            System.out.println("You choose NANO !");
            break;
        case FIAT:
            System.out.println("You choose FIAT !");
  
        default:
            System.out.println("NEW BRAND'S CAR.");
            break;
        }
    }
}

chevron_right


Output:

You choose AUDI !

In the above example, we show how Enum keyword works along with Switch case statements when Enum is declared outside main class.

Example 2: Use of Enum with Switch statement when Enum is within main class

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Show 
// working of Enum keyword
// with Switch statement
  
// Main class
public class MainClass {
  
    // Declaring Enum keyword
    // inside main class
    enum Webseries {
        GOT,
        Breakingbad,
        Lucifer,
        Boys,
        Mirzapur,
        Moneyheist;
    }
    public static void main(String[] args)
    {
        // Declaring and Assigning choice to variable 'wb'
        Webseries wb = Webseries.Mirzapur;
  
        // Switch Keyword
  
        switch (wb) {
  
        // Case statements
        case GOT:
            System.out.println("Game of Thrones selected");
            break;
        case Breakingbad:
            System.out.println("Breaking Bad selected");
            break;
        case Lucifer:
            System.out.println("Lucifer selected");
            break;
        case Boys:
            System.out.println("Boys selected");
            break;
        case Mirzapur:
            System.out.println("Mirzapur selected");
            break;
        case Moneyheist:
            System.out.println("Money Heist selected");
            break;
        default:
            System.out.println("You are outdated !!!");
            break;
        }
    }
}

chevron_right


Output:

Mirzapur selected

In the above example, we show how Enum keyword works along with Switch case statements when Enum is declared within main class.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :