Java program to use enum in switch statement.
I am last year BSc(IT) student. I want to write a Java program to use enum in switch statement. I tried different methods but none of them worked out. Can anybody tell me how to write this program?
Java program to use enum in switch statement.
I am last year BSc(IT) student. I want to write a Java program to use enum in switch statement. I tried different methods but none of them worked out. Can anybody tell me how to write this program?
The enum can be easily use in switch statement in java and use it like a different case to execute. Below example show you how to use enumeration or enum type in a switch statement.
public class MainClass {
enum Choice { Choice1, Choice2, Choice3 }
public static void main(String[] args) {
Choice ch = Choice.Choice1;
switch(ch) {
case Choice1:
System.out.println("Choice1 selected");
break;
case Choice2:
System.out.println("Choice2 selected");
break;
case Choice3:
System.out.println("Choice3 selected");
break;
}
}
}
Last edited by nitesh14; 02-06-2010 at 03:28 PM.
Bookmarks