Modern Java · interview question
In the context of designing an e-commerce application, which of the following is a key advantage of using an enum to represent order statuses over a string?
Multiple choiceMediumchapter-7-curatedpremiummodern-javaenums
Answer and explanation
Correct answer
A. Enums provide compile-time type safety, reducing the chance of invalid values.
Why
Enums provide compile-time type safety, ensuring that only valid order statuses are used. This reduces bugs related to invalid or misspelled status names, which can occur when using strings. With enums, invalid states are caught at compile time rather than runtime.
Test yourself on Modern Java
A free test with instant, explained results. No account needed.
More Modern Java questions
- Given an enum with a method, how can each constant have a different implementation of that method?
- What keyword is used to define a Java Record?
- Which pattern is effectively implemented when using enums to represent singletons?
- How can you iterate over all constants of an enum in Java?
- When designing a domain model, why should you be cautious about using records for classes with business logic?
- How can the use of enums improve the clarity of domain models in Java?