Enumeration is an implicit kind of variable that should not have an uninitialized value.
In fact, your reasoning is true for each type of variable, and not just for enumerations, but in the specific case of enumerations, you declare a type that can have a finite number of possible values, so null should not be an option.
The best solution really has an unknown value:
private enum Shape { TRIANGLE("yellow"), SQUARE("green"), CIRCLE("red"), UNKNOWN("unknown"); private final String color; Shape(String color) { this.color = color; } };
And initialize each variable of type Shape to Shape.UNKNOWN to avoid performing a null check, which is a kind of null design pattern . This template makes even more sense when working with enumerations.
Note: since enumerations represent constants, conventions must give them uppercase names with underscores.
source share