Skip to content

Java Enum

1. How are values stored internally in an Enum? Explain the concept of ordinal.

Section titled “1. How are values stored internally in an Enum? Explain the concept of ordinal.”

Internally, every Enum constant is assigned a sequential index starting from 0.
This index is known as the ordinal value.

Example:

enum Day { MONDAY, TUESDAY, WEDNESDAY }
ConstantOrdinal
MONDAY0
TUESDAY1
WEDNESDAY2
  • The ordinal is used only internally (e.g., in sorting).

  • We should not depend on ordinal values in business logic because they change if order changes.


2. Difference between name(), ordinal(), and valueOf() in an Enum?

Section titled “2. Difference between name(), ordinal(), and valueOf() in an Enum?”
MethodReturnsNotes
name()Constant name as StringValue is fixed & never overridden
ordinal()Index of the constantShould avoid using in logic
valueOf("STRING")Returns Enum constant matching the stringThrows exception if not valid

Example:

Day.MONDAY.name(); // "MONDAY"
Day.MONDAY.ordinal(); // 0
Day.valueOf("TUESDAY"); // returns Day.TUESDAY

Enum.values() returns an array containing all constants of the enum, in the order they are declared.

Example:

for (Day d : Day.values()) {
System.out.println(d);
}

This is commonly used for:

  • Displaying dropdown lists

  • Iterating through enum constants


4. Why can enums have constructors, and why must they be private?

Section titled “4. Why can enums have constructors, and why must they be private?”

Each enum constant is essentially a singleton object of the enum type.
So, if we want extra data inside each constant, we define fields and initialize them through a constructor.

Constructors in Enums are implicitly private because:

  • We cannot create new enum instances manually using new.

  • Only JVM is allowed to create enum instances.


5. How does adding fields to Enum make each constant behave like an object?

Section titled “5. How does adding fields to Enum make each constant behave like an object?”

When we add fields to an Enum:

enum Status {
SUCCESS(200), ERROR(500);
int code;
Status(int code) { this.code = code; }
}

Now:

  • SUCCESS holds code 200
  • ERROR holds code 500

Each Enum constant now behaves like an object with state, not just a constant.

This is powerful in real-world use cases like:

  • Status codes
  • Error types
  • Business workflow states

6. Can we create methods in Enum? If yes, when do we make them static?

Section titled “6. Can we create methods in Enum? If yes, when do we make them static?”

Yes, we can define methods inside Enum.

  • If the method depends on constant-specific values, keep it non-static.

  • If the method is utility-related, not dependent on instance values, make it static.

Example:

enum Day {
MONDAY, TUESDAY;
public boolean isWeekend() { return false; }
}

Static:

public static Day fromCode(int code) { ... }

7. Explain method overriding inside a specific Enum constant.

Section titled “7. Explain method overriding inside a specific Enum constant.”

Enums support constant-specific method implementations:

enum Day {
MONDAY {
@Override
public String message() { return "Start of week"; }
},
SUNDAY {
@Override
public String message() { return "Holiday"; }
};
public abstract String message();
}

Here each Enum constant provides its own implementation, similar to polymorphism.


8. How can an Enum implement an interface? Give practical usage example.

Section titled “8. How can an Enum implement an interface? Give practical usage example.”

Yes, enums can implement interfaces:

interface Printable { void print(); }
enum Color implements Printable {
RED, GREEN, BLUE;
public void print() { System.out.println(this.name()); }
}

Use Case Example: Payment modes implementing a processPayment() method:

enum PaymentMode implements Payable {
UPI { public void pay() { ... } },
CARD { public void pay() { ... } }
}

This allows strategy-like behavior.


9. Why is Enum better than public static final constants?

Section titled “9. Why is Enum better than public static final constants?”
AspectEnum (Recommended)static final constants
Type-safetyStrongly typed (compiler enforces correct usage)Just primitive/String, can pass wrong data
ReadabilitySelf-explanatory groupingScattered constants
ControlCan add methods, fields, behaviorsCannot attach behavior
ExtensibilitySupports polymorphismNo polymorphism
IterationEasy (values())Need manual handling
Singleton behaviorEach constant is a singleton objectNo such guarantee
ComparisonCan use == safelyRisky when using integers/strings

In short:

Enum provides type-safety, readable grouping, and controlled allowed values, making them significantly more robust and maintainable.