Enum Java Interview Questions and Answers
-
What is an enum in Java?
- Answer: An enum (short for enumeration) is a special data type that allows you to define a set of named constants. It's a way to create a type-safe collection of constants, preventing accidental use of invalid values.
-
How do you declare an enum in Java?
- Answer: You declare an enum using the `enum` keyword, followed by the enum's name and a list of its constants within curly braces. For example: `public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }`
-
What are the benefits of using enums?
- Answer: Enums provide type safety, improved readability, and maintainability. They prevent typos and make code easier to understand and modify.
-
Can an enum have methods?
- Answer: Yes, enums can have methods, constructors (private by default), and even implement interfaces.
-
Can an enum have a constructor? If so, what kind?
- Answer: Yes, enums can have constructors, but they must be private. This is because you create enum instances using the defined constants, not directly using `new`.
-
Can an enum implement an interface?
- Answer: Yes, an enum can implement interfaces.
-
Can an enum extend a class?
- Answer: No, enums cannot extend classes. They implicitly extend `java.lang.Enum`.
-
How do you iterate through the constants of an enum?
- Answer: You can use the `values()` method, which returns an array of all the enum constants. For example: `for (Day day : Day.values()) { ... }`
-
How do you get the name of an enum constant?
- Answer: Use the `name()` method. For example: `Day.MONDAY.name()` returns "MONDAY".
-
How do you get the ordinal of an enum constant?
- Answer: Use the `ordinal()` method. This returns the position of the constant in the enum declaration (starting from 0).
-
How do you compare two enum constants?
- Answer: You can use the `==` operator (recommended) or the `.equals()` method. Because enums are singletons, `==` is generally preferred for performance.
-
What is the `valueOf()` method for enums?
- Answer: `valueOf()` is a static method that returns the enum constant corresponding to a given string. For example: `Day.valueOf("MONDAY")` returns the `MONDAY` constant.
-
What happens if you call `valueOf()` with an invalid string?
- Answer: It throws an `IllegalArgumentException`.
-
Can an enum have fields?
- Answer: Yes, enums can have instance variables (fields). These are initialized in the constructor.
-
Explain the difference between using an enum and using `static final` constants.
- Answer: Enums provide type safety, preventing accidental assignment of incorrect values. They also offer built-in methods like `values()` and `valueOf()`. `static final` constants are less type-safe and lack these convenient methods.
-
Can you have an enum within another enum?
- Answer: Yes, you can nest enums.
-
How can you use enums in a switch statement?
- Answer: You can directly use enum constants in `switch` cases. Java's `switch` statement supports enums directly.
-
What is the purpose of the `toString()` method in an enum?
- Answer: The `toString()` method returns a string representation of the enum constant. You can override it to customize the string output.
-
How can you serialize an enum?
- Answer: Enums are serializable by default; no special handling is needed.
Thank you for reading our blog post on 'Enum Java Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!