Quick revision
The short answer
The compiler turns enum Status { ACTIVE, SUSPENDED } into a final class Status that extends java.lang.Enum<Status>, with a public static final field for each constant, all created once in a static initializer, and a private constructor. values() returns a new copy of the constants array in declaration order, valueOf(String) returns the constant with exactly that name or throws IllegalArgumentException, ordinal() is the zero-based position in the declaration, and name() is the declared name, which is final, unlike toString().
What to remember
In this answer, we saw that an enum is a final class extending java.lang.Enum, with one public static final field per constant, all created once in a static initializer through a private constructor that receives the name and the ordinal. values() returns a fresh copy of the constants in declaration order, valueOf() needs the exact name and throws IllegalArgumentException or NullPointerException otherwise, ordinal() is the zero-based position, and name() is the final declared name, unlike toString(). Because constructors are private and reflection and deserialization cannot create instances, every constant is a guaranteed singleton.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The compiler turns enum Status { ACTIVE, SUSPENDED } into a final class Status that extends java.lang.Enum<Status>, with a public static final field for each constant, all created once in a static initializer, and a private constructor. values() returns a new copy of the constants array in declaration order, valueOf(String) returns the constant with exactly that name or throws IllegalArgumentException, ordinal() is the zero-based position in the declaration, and name() is the declared name, which is final, unlike toString().
Why this question is asked
Enums look like a small piece of syntax, but most of what makes them useful, and most of their traps, come from the class the compiler writes for us. Interviewers ask this question to see whether we know that an enum is a real class with real objects, and whether we understand the methods we call every day, such as valueOf() in request parsing or ordinal() in persistence. In this answer, we will look at the generated class, then go through values(), valueOf(), ordinal() and name() with the result of each call, and finally see why nobody can create a new Status at run time.
1A Quick Recap: An Enum Is a Class
In some languages an enum is just a set of named integers. In Java, an enum is a class whose instances are fixed when the class is written. Each constant is an object of that class, so it can have fields, methods and constructors, and it can implement interfaces. All enum classes extend java.lang.Enum, which provides name(), ordinal(), compareTo() and the serialization behaviour.
Because we cannot see the generated code in our source, it helps to write out roughly what javac produces. We can confirm it ourselves with javap -p Status.class.
- First use of Statusfor example
Status.ACTIVEorStatus.values() - JVM initializes the classruns the static initializer once, thread-safely
- Create ACTIVEname "ACTIVE", ordinal 0
- Create SUSPENDEDname "SUSPENDED", ordinal 1
- Build $VALUESthe private array behind
values()
5 steps in order. Play them, or step through them one at a time.
Notice the order inside the static initializer: the constants are created first, in declaration order, and the $VALUES array is built after them. The constructor receives the name and the ordinal from the compiler, which is where name() and ordinal() get their values. The class is final, so nobody can subclass it, unless one of the constants has its own class body, in which case the enum is implicitly sealed and only those constant bodies may extend it.
2Values(): A Fresh Copy Every Time
values() returns all constants in declaration order. What surprises people is that it returns a new array each time. The private $VALUES array must stay intact, because valueOf() and other code depend on it, and arrays in Java are always mutable. If values() returned the array itself, any caller could overwrite an element. So it returns a clone.
So values() allocates on every call. That is harmless in ordinary code, but in a hot loop it can show up in a profiler, and caching the result in a private static final field is the usual fix.
3ValueOf(): An Exact Name or an Exception
valueOf(String) turns a name into a constant. It matches the declared name exactly, including case, and it does not trim spaces. A name that does not match throws IllegalArgumentException, and a null name throws NullPointerException.
This matters whenever the name comes from outside, such as a request parameter or a file. Calling valueOf() directly on user input turns a typo into an unhandled exception and, in a web application, often a 500 error. A small parse method that normalizes the text and maps unknown values to a clear error or an Optional.empty() is safer.
4Ordinal() and name(): Position and Declared Name
ordinal() returns the zero-based position of the constant in the declaration: ACTIVE is 0 and SUSPENDED is 1. compareTo() between constants uses this order. name() returns the declared name exactly as written in the source.
The difference between name() and toString() is deliberate. toString() is for humans and we may override it to print something friendly. name() is final in Enum, so it always returns the identifier that valueOf() accepts. That makes name() the right choice whenever the value must be read back later. ordinal(), by contrast, changes whenever someone inserts or reorders constants, so it should never be stored in a database or sent to another system.
5Why Nobody Can Create Another Status
Enum constructors are always private. Writing public on an enum constructor is a compile error, and new Status() is rejected anywhere. The JDK also blocks the back doors. Reflection fails, because Constructor.newInstance() refuses enum classes, and deserialization looks up the existing constant by name instead of creating a new object.
So the set of constants is truly fixed, and each constant is a singleton. That is the guarantee that lets us compare enums with ==, lets EnumSet and EnumMap index by ordinal, and makes the enum singleton pattern safe.
6Follow-up questions
Is the enum class always final?
It is implicitly final when no constant has a class body. If a constant declares its own body, for example to override a method, the compiler generates an anonymous subclass for that constant, and since Java 17 the enum class is implicitly sealed, permitting only those subclasses. Either way, no other code can extend it.
When does the static initializer that creates the constants run?
When the class is first initialized, typically on the first access to a constant, values() or valueOf(). The JVM runs class initialization exactly once and makes it thread-safe, so all threads see fully constructed constants.
Why does Enum implement Comparable and Serializable?
Comparable lets constants sort in declaration order, which EnumSet and sorted collections use. Serializable lets constants travel through Java serialization, where only the name is written and the existing constant is looked up on read, so singletons stay singletons.
7Conclusion
In this answer, we saw that an enum is a final class extending java.lang.Enum, with one public static final field per constant, all created once in a static initializer through a private constructor that receives the name and the ordinal. values() returns a fresh copy of the constants in declaration order, valueOf() needs the exact name and throws IllegalArgumentException or NullPointerException otherwise, ordinal() is the zero-based position, and name() is the final declared name, unlike toString(). Because constructors are private and reflection and deserialization cannot create instances, every constant is a guaranteed singleton.
Open with
The compiler turns that enum into a final class Status that extends java.lang.Enum of Status. It has a public static final field for ACTIVE and one for SUSPENDED, and a private static array of all constants.
Then, if they want more
A static initializer, run once when the class is initialized, calls a private constructor with each constant's name and ordinal, and then fills the array. values() returns a clone of that array in declaration order, so every call allocates a new array, which protects the internal one from modification. valueOf takes the exact declared name, case and spaces included, and returns that constant; an unknown name throws IllegalArgumentException and null throws NullPointerException, so I wrap it when parsing user input. ordinal() is the zero-based declaration position, so ACTIVE is zero; it changes if someone reorders constants, which is why I never persist it. name() returns the declared name and is final, unlike toString, which can be overridden for display, so name is what I use when the value must be read back.
Enum constructors are always private, and reflection throws when asked to create an enum instance, so each constant is a true singleton and == is safe for comparing them.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does the compiler generate for enum Status { ACTIVE, SUSPENDED }? Explain values()...": https://scrivio-interview-library.netlify.app/java/enums/595c5836df/