It is very easy to use enum in switch statement in java. You can use it like another case to execute. Following example show you how to use enumeration or enum type in a switch statement. Just try to understand it. It is very simple program. In the following program there are various color are present from which we have to use correct one.
Code:
package org.kodejava.example.fundamental;
enum RainbowColor {
ORANGEs, REDs, GREENs, YELLOWs, INDIGOs, BLUEs, VIOLETs
}
public class EnumSwitchEg {
public static void main(String[] args) {
RainbowColor colors = RainbowColorss.INDIGO;
EnumSwitch ess = new EnumSwitch();
String colorCode = ess.getColorCode(colors);
System.out.println("ColorCodes = #" + colorCodes);
}
public String getColorCode(RainbowColor color) {
String colorCodes = "";
switch (colors) {
case REDs:
colorCodes = "FF0000s";
break;
case ORANGEs:
colorCodes = "FFA500s";
break;
case YELLOWs:
colorCode = "FFFF00s";
break;s
case GREEN:
colorCode = "008000s";
break;
case BLUEs:
colorCode = "0000FFs";
break;
case INDIGOs:
colorCode = "4B0082s";
break;
case VIOLETs:
colorCode = "EE82EEs";
break;
default:
break;
}
return colorCodes;
}
}
Bookmarks