Java Enum Interview Questions and Answers
This page provides answers to frequently asked Java Enum interview questions.
What is an Enum?
An enum (short for enumeration) allows a type to define a set of named constants. For example:
Syntax
enum Animals {
dog, lion, cat, fish
}
This defines an Animals
enum with four possible values.
Enum Ordinal
Java assigns ordinal numbers (integers starting from 0) to enum constants in the order they're declared. While not ideal for logic, they are available:
Example
System.out.println(Animals.dog.ordinal()); // 0
System.out.println(Animals.lion.ordinal()); // 1
Using Switch Statements with Enums
Enums work seamlessly with switch statements:
Example
public int num() {
switch (this) {
case dog: return 5;
case lion: return 8;
// ... more cases
default: return -1;
}
}
Purpose of Enums
Enums are ideal for defining lists of constants, especially when there are a limited number of possible values.
Static and Final Enum Constants
Yes, enum constants are implicitly public
, static
, and final
.
Adding Elements to an Enum
No, you cannot add elements to an enum after its definition. The elements are fixed at compile time.
Benefits of Using Enums
- Improved type safety
- Readability and maintainability
- Easy use in switch statements
- Can't be extended at runtime
- Can't be instantiated directly
- Can have constructors, methods, and fields
Enum's ordinal()
Method
The ordinal()
method returns the position of an enum constant in its declaration order (starting from 0).
Enums and Interfaces
Yes, enums can implement interfaces.
Creating an Enum Without Instances
It's possible to create an enum without any instances, often used for utility classes. This is an advanced usage scenario.
Overriding toString()
in Enums
Yes, you can override the toString()
method. If not overridden, the default implementation returns the enum constant's name.
Creating Enum Instances Outside the Enum Definition
No, you cannot create enum instances outside the enum declaration. The compiler manages enum instance creation.
Private Constructor in Enum
You can define a private constructor within an enum, but public and protected constructors are not allowed.
Enums with TreeSet
or TreeMap
Yes, enums can be used with TreeSet
and TreeMap
because they implicitly implement the Comparable
interface.
ordinal()
vs. compareTo()
compareTo()
mirrors the ordering of ordinal()
, comparing enum constants based on their declaration order. Comparison is only valid within the same enum type.
Enums Extending Classes
No, enums in Java cannot extend classes (except implicitly extending java.lang.Enum
).
Looping Through Enum Instances
Use the values()
method to get an array of all enum constants and then iterate using a loop (e.g., a for-each loop).
Enums as Singletons
Enums provide a convenient and thread-safe way to implement the Singleton pattern, avoiding complexities like double-checked locking.
Enum Pattern vs. Enum Int/String Patterns
The modern enum pattern (using the enum
keyword) is superior to older approaches (using public static final
constants) due to improved type safety and compiler enforcement.
Converting String to Enum
A common approach is to create a static factory method within the enum that takes a String and returns the corresponding enum constant (or handles invalid input).