Enums

Some values come from a small, fixed set: the days of the week, the suits in a deck, a traffic light's colour. An enum turns that set into its own type, so the compiler guarantees a value is always one of the allowed options — never a typo'd String or a meaningless int.

Learn Enums in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Imagine representing a traffic light with a String :

Nothing stops you from assigning "banana" . The bug only appears when the program runs. An enum closes that hole — the value can only be one of the constants you declared, and the compiler catches anything else:

💡 Analogy: A String is a blank form where anyone can scribble anything. An enum is a drop-down menu — the user can only pick from the options you provided.

The natural way to act on an enum is a switch . Inside a switch over an enum you write the bare constant name — case RED , not case TrafficLight.RED . The compiler can even warn you if you forget a case.

This arrow form is a switch expression — it produces a value you can assign directly. You'll study switch fully in the next lesson; enums are its ideal companion.

This is where enums become genuinely powerful. Each constant can carry its own data , supplied through a constructor, and the enum can define methods shared by every constant. A classic example is planets, each with a mass and radius and a method that computes surface gravity.

The constructor runs once per constant when the enum is first used. The fields are typically final so each constant's data never changes — enums should be immutable.

Reorder these lines so the program declares a Size enum and prints the ordinal of LARGE .

Why: The enum type must be declared before a variable of that type can exist, and pick must be assigned before you read its ordinal() . LARGE is the third constant, so the output is 2 (ordinals are 0-based).

2 . Ordinals are 0-based: RED=0, GREEN=1, BLUE=2.

3 . values() returns an array of all three constants, so its length is 3.

3. Is Color.RED == Color.RED a safe comparison?

Yes — it prints true . Each enum constant is a singleton, so == works correctly (and is null-safe), unlike with Strings.

You can now model a fixed set of options as a type-safe enum : declare constants, compare them with == , use name() / ordinal() / values() , drive a switch with them, and even attach data and methods to each constant.

Next up: Switch Statement & Switch Expressions — branch cleanly on many values, including the enums you just learned.

Practice quiz

What is an enum?

  • A resizable list
  • A key-value store
  • A type whose value is one of a fixed set of named constants
  • A kind of loop

Answer: A type whose value is one of a fixed set of named constants. An enum defines a fixed, named set of constants the value can only be one of.

What does ordinal() return for an enum constant?

  • Its 0-based position in declaration order
  • Its name as text
  • Its 1-based position
  • Its hash code

Answer: Its 0-based position in declaration order. ordinal() is the zero-based position in declaration order.

For enum Color { RED, GREEN, BLUE }, what is Color.BLUE.ordinal()?

  • 1
  • 3
  • 0
  • 2

Answer: 2. Ordinals are 0-based: RED=0, GREEN=1, BLUE=2.

What does values() return?

  • The number of constants
  • An array of all the enum's constants
  • The first constant
  • A sorted list of names

Answer: An array of all the enum's constants. values() returns an array of every constant, useful for looping.

For enum Color { RED, GREEN, BLUE }, what is Color.values().length?

  • 3
  • 2
  • 0
  • 1

Answer: 3. There are three constants, so the array length is 3.

Is comparing enums with == safe?

  • No, use equals()
  • Only for the first constant
  • Yes — each constant is a singleton and == is null-safe
  • Only inside a switch

Answer: Yes — each constant is a singleton and == is null-safe. Each enum constant is a single shared instance, so == works correctly and is null-safe.

Inside a switch over an enum, how do you write a case label?

  • case TrafficLight.RED
  • case RED
  • case "RED"
  • case RED()

Answer: case RED. Use the bare constant name (case RED); qualifying it is a compile error.

Can an enum have fields, a constructor, and methods?

  • No, only constants
  • Only static methods
  • Only if it implements an interface
  • Yes — each constant can carry data and the enum can define methods

Answer: Yes — each constant can carry data and the enum can define methods. Enums can have fields passed to a constructor and methods shared by all constants (e.g. Planet).

What does Day.valueOf("funday") do when no such constant exists?

  • Returns null
  • Throws IllegalArgumentException
  • Returns the first constant
  • Creates a new constant

Answer: Throws IllegalArgumentException. valueOf throws IllegalArgumentException if the name does not match a constant exactly.

When an enum has fields or methods, what must follow the list of constants?

  • A comma
  • Nothing
  • A semicolon (;)
  • The 'end' keyword

Answer: A semicolon (;). The constant list must end with a semicolon before any fields or methods.