Home > Tutorial > Java Enum Data Type Sample

Java Enum Data Type Sample

What is an Enum? Sounds like some sort of animal like an Emu. Maybe not. A Java Enum is a special data type that enables a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

The days of the week, directional, months of the year, states in the union are all good examples of where an Enum makes sense. Remember that the Enums are constants, therefore they are defined as uppercase letters. Take a look at the two example Enums below:

    //Let's define an Enum for Days of Week
	enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
	
	//Let's define an Enum for Directions
	enum Directions { NORTH, SOUTH, EAST, WEST }


Defining Constants Before Enum

Before the introduction of Enum data type in Java 1.5, your only option to define a constant was using a static final variable. While this works, it’s not very meaningful assigning days of the week to integer values.

    public static final int MONDAY=1;
	public static final int TUESDAY=2;
	public static final int WEDNESDAY=3;
	public static final int THURSDAY=4;
	public static final int FRIDAY=5;
	public static final int SATURDAY=6;
	public static final int SUNDAY=7;


Enum Switch Sample Code

Use Enum data types any time you need to portray a fixed set of constants. It is important that you know all possible values at compile time. Remember these are all possible constant values.

public static void JCDEnumExample(Days day) {
		
		switch(day) 
		{
			case MONDAY:
				System.out.println("Good Grief it's Monday!");  
				break;
	        case TUESDAY:
	        	System.out.println("Tuesday is not Monday!");
	        	break;
	        case WEDNESDAY:
	        	System.out.println("Wednesday hump day!");
	        	break;
	        case THURSDAY:
	        	System.out.println("Thursday Getting Closer to Weekend!");
	        	break;
	        case FRIDAY:
	        	System.out.println("Friday Can Feel Weekend!");
	        	break;
	        case SATURDAY:
	        	System.out.println("Saturday Sleep In!");
	        	break;
	        case SUNDAY:
	        	System.out.println("Sunday Church!");
	        	break;
	        default:
	        	System.out.println("So Confused");
	        	break;
		}
		
	}


Enum Iteration Sample Code

You can easily iterate through the Enum values using an enhanced for loop.

public static void JCDEnumIteration() {
			
		Days[] days = Days.values();
		
		System.out.println("Days of Week Enum:\n");
	    
	    for (Days day : days) 
	    {
	    	System.out.println(day.toString());	   
	    }
	    
	}


Enum Iteration Sample Code Output

Days of Week Enum:

SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY


Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot