Can I set enum start value in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, enums (short for enumerations) are a special data type that enables a variable to be a set of predefined constants. Enums are useful when you have a fixed set of related constants, such as days of the week, states of a process, etc. Java enums, introduced in JDK 1.5, are more powerful than the typical enum types you might encounter in other programming languages because they are represented as a class with methods and fields.
Can You Set the Start Value of an Enum?
Unlike some other programming languages, Java enums do not have values that can be set explicitly, such as integers that start from a specific value. Enums in Java consist of named constants, which are instances of the enum class. By default, the constants are determined by their ordinal values, starting from 0.
Ordinal Values
In Java, each enum constant has an ordinal() method, which returns its position in its enum declaration, where the initial constant is assigned an ordinal of zero. The ordinal() method is primarily used for ordering operations.
Example of Java Enum
Here's how a basic enum looks in Java:
In this example, SUNDAY has an ordinal of 0, MONDAY is 1, and so forth.
Assigning Specific Values to Enums
While you can't change the behavior of the ordinal() method, you can create an enum with associated values by defining fields within the enum class. Here's how you can achieve this:
Explanation
In the above code:
- Each constant of the enum
Dayis associated with an integer value. - The constructor
private Day(int value)initializes thevaluefor each enum constant. - The method
getValue()returns the integer associated with each enum constant.
With this setup, you can now access the specific integer value for each day:
The output of the above program would be:
Use Cases for Custom Values
By associating specific values with each constant, you can enhance the functionality and readability of your code. This approach is particularly useful in scenarios like:
- Mapping enums to database values: In cases where days or states need to correspond to specific database entries.
- Custom sorting mechanisms: When a natural order dictated by the ordinal does not suffice.
- Switch cases requiring specific values: When decisions in switch statements depend on non-ordinal values.
Summary Table
| Feature | Description |
| Ordinal Values | Default ordering based on the position in definition |
| Custom Values | Possible by defining fields and constructors |
| Constructors | Initialize custom values within the enum |
| Methods | Provide access methods like getValue() to retrieve values |
| Use Cases | Database mapping, custom sorting, switch cases |
Conclusion
While Java enums inherently do not support explicit starting values through the ordinal() method, you can associate any data with enums via custom fields and constructors. This allows for versatile and powerful use, accommodating a wide range of applications beyond mere ordinal storage. The balance between simplicity and the potential for added functionality makes the enum a compelling feature in Java programming.
Further Reading
- Effective Java by Joshua Bloch - A recommended read for design patterns, including the use of enums in Java.

