HuntExams Academy logo
JavaBeginner#fundamentals

What is the difference between an enum and a class with constants?

Answer

An enum is a special class representing a fixed set of constants with type safety, can have fields, methods, and constructors, and works with switch statements. Plain constants (e.g., static final int) lack type safety and namespace grouping.

Example

enum Day { MON, TUE, WED }
Day d = Day.MON;
switch (d) {
  case MON -> System.out.println("Monday");
  default -> System.out.println("Other");
}

Detailed explanation

In Java, an enum defines a distinct type whose values are a fixed set of named instances, such as enum Day { MON, TUE, WED }. A class with constants usually exposes fields such as public static final int MON = 1. The key difference is type safety: a method accepting Day accepts a Day value, not an arbitrary integer or a constant from an unrelated category. A method accepting int cannot make that distinction without validation. This makes enums useful for statuses, directions, and other closed sets of choices.

Under the hood, an enum is a special class that implicitly extends java.lang.Enum. Each declared constant is a public static final instance of that enum type, created during class initialisation. You cannot create additional instances using new or extend an enum with another class. Enums can implement interfaces and contain fields, methods, and constructors. Their constructors cannot be public or protected, and are invoked for the declared constants. For example, each Day constant could store a display label. Prefer final fields when these shared instances should remain immutable.

Enums also provide built-in operations: values() returns the constants in declaration order, valueOf() finds a constant by its exact declared name, and name() returns that name. Enum references can be compared with == because each declared constant has a unique identity. Enums work naturally with switch; Java also supports switch over compatible integer and String constants, so switching is not exclusive to enums. The arrow-style switch in the example requires Java 14 or later as a standard feature. A Day variable can still be null, so type safety does not eliminate null handling.

A constants class also groups names, for example DayConstants.MON, so saying it lacks namespace grouping is inaccurate. What ordinary int or String constants lack is a dedicated type restricting valid choices. Such constants remain appropriate for independent values like MAX_RETRIES or DEFAULT_TIMEOUT_MS. In interviews, explain the invalid-input problem first, then discuss enum behaviour and built-in support. Also avoid using ordinal() as a database identifier: changing declaration order changes ordinal values.

Key points to remember

  • An enum defines a dedicated type with a fixed set of named instances.
  • Ordinary int or String constants cannot prevent unrelated values of the same underlying type.
  • Enums support fields, methods, restricted constructors, and interface implementation.
  • Both enums and constants classes group names; both can supply switch cases where supported.
  • Use enums for closed choices and ordinary constants for independent configuration values.

Common mistakes candidates make

  • Claiming constants classes provide no namespace grouping or cannot be used with switch.
  • Assuming an enum variable cannot be null or that enum fields are automatically immutable.
  • Persisting ordinal() as a stable business identifier instead of defining an explicit code.

Likely follow-up questions

Can an enum extend a class or implement an interface?

An enum cannot explicitly extend another class because it already extends java.lang.Enum. It can implement interfaces and provide implementations through shared methods or constant-specific bodies.

Should enum values be compared using == or equals()?

Both compare enum identity, but == is concise and safe when either reference is null. Calling equals() on a null reference throws NullPointerException.

How do you convert a String into an enum value?

Use Day.valueOf("MON") for an exact, case-sensitive match to a declared constant name. An unknown name throws IllegalArgumentException, so external input should be validated or handled with a suitable parsing method.

Interview tip: Contrast a method accepting int with one accepting Day, then explain how the enum rejects unrelated values at compile time.

Related Interview Questions

1
JavaIntermediate#fundamentals#java14

What are records in Java (Java 14+)?

Open
2
JavaAdvanced#fundamentals#java17

What is the difference between sealed classes and final classes (Java 17)?

Open
3
JavaIntermediate#fundamentals#java14

What is the difference between switch statement and switch expression (Java 14+)?

Open