skip to content

What is an enum in Java, and how do you declare one with a fixed set of constants?

level: juniorimportance: must knowfreq 80%

answer

  1. Fixed, finite set of named constant instances
  2. enum keyword; each constant is public static final, created once
  3. Implicitly extends java.lang.Enum -> no other superclass, but can implement interfaces
  4. Type-safe: compiler rejects ints/strings
  5. Compare with == (one singleton per constant)

basics

~20 s

An enum is a special class whose instances are a fixed, named set of constants. You declare it with the enum keyword and list the constants, e.g. enum Day { MONDAY, TUESDAY }. Each constant is one shared, type-safe object.

solid answer

~40 s

An enum is a special class type that defines a fixed, finite set of named constant instances. You write `enum Direction { NORTH, SOUTH, EAST, WEST }`. Each listed name is a public static final instance of that enum type, created once by the JVM. Because the set is closed and type-safe, you cannot pass an arbitrary int or string where a Direction is expected, and the compiler can check switch exhaustiveness. Enums are far safer than the old `public static final int` constant pattern: they have a real type, meaningful toString, and cannot collide or be confused with unrelated constants. Under the hood each enum implicitly extends java.lang.Enum, so it cannot extend another class, but it can implement interfaces. You reference a constant by name (Direction.NORTH) and compare them with ==.

go deeper

for a junior

Can declare a simple enum and reference a constant by name; knows enums replace magic int/string constants and are type-safe.

for a middle

Explains that each constant is a public static final singleton instance, that the enum extends java.lang.Enum, and why == is the right comparison.

for a senior

Articulates the design rationale (closed type, compiler-checked switch, no spoofed values), the single-inheritance constraint vs interface implementation, and null-safety of ==.

for a principal

Frames enums in API and domain-modeling decisions: when a closed enum vs an open extensible type is appropriate, serialization/versioning implications of adding constants, and migration from legacy int-constant code.

## What problem enums solve Before enums (Java 5, 2004), people represented a fixed set of choices with integer or string constants, the **int enum pattern**: ```java public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; ``` This is unsafe. The compiler sees only an `int`, so you can pass `99` or add two seasons together, and nothing complains. There is no namespace, no type, and printing the value shows a meaningless number. ## What an enum is An **enum** (short for *enumeration*) is a special kind of **class** whose set of instances is **fixed and known at compile time**. You declare it with the `enum` keyword and list the **constants** (the instances) by name: ```java public enum Season { WINTER, SPRING, SUMMER, FALL } ``` Each name — `WINTER`, `SPRING`, etc. — is automatically a **`public static final` instance** of type `Season`. The JVM creates exactly one object per constant, once, when the enum class is loaded. So `Season.WINTER` always refers to the same single object (it is a built-in singleton per constant). Key terms: - **Constant**: one of the named instances (`WINTER`). - **Type-safe**: the compiler enforces that only a `Season` value can be used where a `Season` is expected; you cannot substitute an int or a string. - **Instance**: a concrete object; here, each constant is one. ## Why it is a class, not just a list `Season` compiles to a real class that implicitly **extends `java.lang.Enum`**. Because Java has single inheritance of classes, an enum therefore **cannot extend any other class** — but it **can implement interfaces**. Every enum inherits useful behavior from `Enum`: a sensible `toString()` (returns the constant name), `name()`, `ordinal()`, and natural ordering. ## Using an enum ```java Season s = Season.WINTER; // reference a constant by name if (s == Season.WINTER) { ... } // compare with == (safe: one instance each) System.out.println(s); // prints WINTER (inherited toString) ``` You compare enum constants with `==` rather than `equals()` because each constant is a unique singleton — `==` is both correct and null-safe (`s == Season.WINTER` is false, not an NPE, when `s` is null). ## Where enums fit syntactically An enum is a top-level type (its own `.java` file if public) or a nested/member type. Its constant declarations must be the **first thing** in the body, ending with a semicolon if anything else (fields, methods) follows. ## Summary An enum gives you a closed, type-safe, self-documenting set of named singleton constants backed by a real class — replacing fragile int/string constants and enabling compiler-checked switches.

  • Why is == safe for comparing enum constants when it is risky for most objects?
    Because each enum constant is a single shared instance created once by the JVM, so reference equality (==) coincides with logical equality. It is also null-safe: comparing a null reference with == yields false instead of throwing NullPointerException, whereas null.equals(...) would throw.
  • Can an enum implement an interface?
    Yes. An enum already implicitly extends java.lang.Enum so it cannot extend another class, but it may implement one or more interfaces and provide the implementing methods, either shared across all constants or as constant-specific bodies.

saying these in an interview costs you the question

  • Calling an enum 'just a list of int constants' — it is a full class with type safety
  • Claiming an enum can extend another class (it cannot; it already extends Enum)
  • Saying you must use equals() to compare constants (== is correct and null-safe)
  • Thinking new instances of an enum can be created at runtime

context