Enum in Switch Case in Java

Introduction

 
Java 5 introduces a new feature known as enums. We can also do switching using enums. The Switch case works only with int, short, char and byte values but using enums this problem is eliminated.
 

Enums

 
The following describes enums:
  • Enums are introduced in Java 5.
  • These are data types in Java that contain fixed values in Java.
  • These can be used for months, categories, names and so on.
  • Type safety is also improved with the introduction of enums.
  • Functions, methods and interfaces are also implemented by enums.
  • Enums can also be used in switch case in Java.
Example
  1. package pkgenum;    
  2.         
  3. public class Enum    
  4. {  
  5.         public enum Friends { raj, ram, aj, nick, mike, mj, jj}    
  6.         
  7.         public static void main(String[] args)    
  8.         {    
  9.             for(Friends f : Friends.values())    
  10.                 System.out.println(f);   
  11.         }   
  12. }  
Output  
 
enum.jpg

Switch Case

 
The following describes the Switch Case:
  • Supports int, short, char, byte data types only.
  • Is a substitute for the else-if ladder
  • Is used to control the flow of our program.
Syntax
 
switch (expression)
{
    case 1:
    {
        statements...
        break;
    }
   case 2:
    {
        statements...
        break;
    }
    default:
    {
        statements...
        break;
    }
}
 
Example
  1. package pkgswitch;  
  2.   
  3. public class Switch  
  4. {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         int age = 21;  
  8.         switch (age)  
  9.         {  
  10.             case 20:  
  11.                 {  
  12.                     System.out.println("twenty");  
  13.                     break;  
  14.                 }  
  15.             case 21:  
  16.                 {  
  17.                     System.out.println("twenty one");  
  18.                     break;  
  19.                 }  
  20.             case 22:  
  21.                 {  
  22.                     System.out.println("twenty two");  
  23.                     break;  
  24.                 }  
  25.             case 23:  
  26.                 {  
  27.                     System.out.println("twenty three");  
  28.                     break;  
  29.                 }  
  30.             default:  
  31.                 {  
  32.   
  33.                     System.out.println("out of range");  
  34.                 }  
  35.         }  
  36.     }  

Output
 
switch case.jpg 
 

Enum in Switch Case

 
Enums can be used in switch case as follows:
  1. package demo;  
  2.   
  3. public class Demo  
  4.   
  5. {  
  6.    public enum Month  
  7.    {  
  8.         JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC  
  9.    }  
  10.   
  11.    public static void main(String args[])  
  12.    {  
  13.        Month[] MonthNow = Month.values();  
  14.        for (Month Now : MonthNow)  
  15.        {  
  16.             switch (Now)  
  17.             {  
  18.                 case JAN:  
  19.                 {     
  20.                     System.out.println("JANUARY");  
  21.                     break;  
  22.                 }  
  23.                 case FEB:  
  24.                 {  
  25.                     System.out.println("FEBRUARY");  
  26.                     break;  
  27.                 }  
  28.                 case MAR:  
  29.                 {  
  30.                     System.out.println("MARCH");  
  31.                     break;       
  32.                 }  
  33.                 case APR:  
  34.                 {  
  35.                     System.out.println("APRIL");  
  36.                     break;  
  37.                 }  
  38.                 case MAY:  
  39.                 {  
  40.                     System.out.println("MAY");  
  41.                     break;  
  42.                 }  
  43.                 case JUN:  
  44.                 {  
  45.                     System.out.println("JUNE");  
  46.                     break;  
  47.                 }  
  48.                 case JUL:  
  49.                 {  
  50.                     System.out.println("JULY");  
  51.                     break;  
  52.                 }  
  53.                 case AUG:  
  54.                 {  
  55.                     System.out.println("AUGUST");  
  56.                     break;  
  57.                 }  
  58.                 case SEP:  
  59.                 {  
  60.                     System.out.println("SEPTEMBER");  
  61.                     break;  
  62.                 }     
  63.                 case OCT:  
  64.                 {  
  65.                     System.out.println("OCTOBER");  
  66.                     break;  
  67.                 }  
  68.                 case NOV:  
  69.                 {  
  70.                     System.out.println("NOVEMBER");  
  71.                     break;  
  72.                 }  
  73.                 case DEC:  
  74.                 {  
  75.                     System.out.println("DECEMBER");  
  76.                     break;  
  77.                 }     
  78.             }  
  79.         }  
  80.     }  

Output
 
enum in switch case.jpg 
 

Summary

 
This article will introduce you to enums and switch cases in Java. This also provided an example of an enum in a switch case.


Similar Articles