Why use an enum instead of String constants? Does an enum have a constructor, and how do you parse one from input?
Short answer
An enum is a fixed set of values (like order statuses) checked by the compiler. Each constant has exactly one object, so you compare with ==.
3 points to say:
valueOfis case-sensitive and throws on a wrong name.- Bad user input returns 400, not 500.
- Store the name or a code in the database, never
ordinal().
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.