← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 4 of 10

Java Enum

An enum is a fixed list of named choices, like days of the week or traffic light colors. It stops you from using something that is not on the list.
Syntax
java
enum EnumName {
    CONSTANT1, CONSTANT2, CONSTANT3
}

EnumName variable = EnumName.CONSTANT1;

What is an Enum?

An enum defines a fixed, named set of constant values, like DAY or SUIT, giving you compile-time safety that a variable can only ever hold one of those specific values.

Because the compiler restricts a variable of this enum type to only its declared constants, invalid values become impossible rather than just discouraged.

Example: What is an Enum?

java
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Day today = Day.MONDAY;
		// Print a line to the console
		System.out.println(today);
	}
}

Enum with Fields and Methods

Enum constants can carry their own fields and methods, letting each constant hold associated data (like a planet's mass) and expose behavior specific to that constant.

This turns an enum into more than just a labeled constant, effectively giving each value its own mini-object with real behavior.

Example: Enum with Fields and Methods

java
// Define the enum Planet (a fixed set of constants)
enum Planet {
	EARTH(5.97), MARS(0.64);
	final double massInYottagrams;
	Planet(double mass) { this.massInYottagrams = mass; }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Planet.EARTH.massInYottagrams);
	}
}

Iterating over Enums

You can iterate every constant in an enum with EnumType.values(), which returns them as an array in the order they were declared.

This is especially useful when you need to populate a dropdown, generate a report, or loop through every possible state a variable could hold.

Example: Iterating over Enums

java
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		for (Day d : Day.values()) {
			// Print a line to the console
			System.out.println(d);
		}
	}
}

Enum valueOf and ordinal

valueOf(String) converts a matching string back into its enum constant, throwing IllegalArgumentException if there's no match; ordinal() returns each constant's zero-based declaration position.

Example: Enum valueOf and ordinal

java
enum Day { MONDAY, TUESDAY, WEDNESDAY }
public class Main {
	public static void main(String[] args) {
		Day d = Day.valueOf("TUESDAY");
		System.out.println(d.ordinal()); // 1: zero-based position
	}
}

EnumSet and EnumMap

EnumSet and EnumMap are highly optimized collection implementations built specifically for enum keys, offering better performance than a generic HashSet/HashMap would for the same data.

Example: EnumSet and EnumMap

java
// Import java.util.EnumSet so it can be used by its short name
import java.util.EnumSet;
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		EnumSet<Day> weekStart = EnumSet.of(Day.MONDAY, Day.TUESDAY);
		// Print a line to the console
		System.out.println(weekStart);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using new to create an enum value, as in new Day(), which is a compile error.
  2. Using valueOf() with a wrong name, which throws IllegalArgumentException.
  3. Trying to create an enum instance with new, which is a compile error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.