Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaUsage of Enum and Switch Keyword in Java

Usage of Enum and Switch Keyword in Java

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, iteration, etc. 

  • enum constants are public, static, and final by default.
  • enum constants are accessed using dot syntax.
  • An enum class can have attributes and methods, in addition to constants.
  • You cannot create objects of an enum class, and it cannot extend other classes.
  • An enum class can only implement interfaces.

Example:

Java




// 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);
    }
}


Output

FRIDAY

Switch Keyword

The 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




// 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");
        }
    }
}


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




// Java Program to show the
// working of Enum keyword
// with Switch statement
 
// Enum keyword declared outside main class
enum Cars {
    BMW,
    JEEP,
    AUDI,
    VOLKSWAGEN,
    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 VOLKSWAGEN:
            System.out.println("You choose VOLKSWAGEN !");
            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;
        }
    }
}


Output

You choose AUDI !

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

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

Java




// 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;
        }
    }
}


Output

Mirzapur selected

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments