Usage of Enum and Switch Keyword in Java
Last Updated :
28 Feb, 2022
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
enum Days {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
public class temp {
public static void main(String[] args)
{
Days x = Days.FRIDAY;
System.out.println(x);
}
}
|
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
public class Example_Switch {
public static void main(String[] args)
{
int num = 50 ;
switch (num) {
case 10 :
System.out.println( "10" );
break ;
case 20 :
System.out.println( "20" );
break ;
case 30 :
System.out.println( "30" );
break ;
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
enum Cars {
BMW,
JEEP,
AUDI,
VOLKSWAGEN,
NANO,
FIAT;
}
public class Main {
public static void main(String args[])
{
Cars c;
c = Cars.AUDI;
switch (c) {
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 ;
}
}
}
|
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
public class MainClass {
enum Webseries {
GOT,
Breakingbad,
Lucifer,
Boys,
Mirzapur,
Moneyheist;
}
public static void main(String[] args)
{
Webseries wb = Webseries.Mirzapur;
switch (wb) {
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 ;
}
}
}
|
In the above example, we show how the Enum keyword works along with Switch case statements when Enum is declared within main class.
Share your thoughts in the comments
Please Login to comment...