devxlogo

Using Enum Effectively

Using Enum Effectively

Learn how to use enum effectively in a switch case block. There are multiple advantages to not making any mistakes since the originals are defined in the enum.

public class EnumColors {public static void main(String args[]){Color color = Color.WHITE;String colorArg = (args.length == 0) ? "White" : args[0];if (args.length == 0)System.out.println("No Color provided. Hence assigning WHITE");if (colorArg != ""){if (colorArg.equalsIgnoreCase("Saffron"))color = Color.SAFFRON;else if (colorArg.equalsIgnoreCase("White"))color = Color.WHITE;else if (colorArg.equalsIgnoreCase("Green"))color = Color.GREEN;}switch(color) {case SAFFRON:System.out.println("Saffron Color");break;case WHITE:System.out.println("White Color");break;case GREEN:System.out.println("Green Color");break;}}}enum Color {SAFFRON,WHITE,GREEN}

Expected output:

[root@mypc]# java EnumColorsNo Color provided. Hence assigning WHITEWhite Color[root@mypc]# java EnumColors whiteWhite Color[root@mypc]# java EnumColors greenGreen Color
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist