Differences:
- Enums extend
java.lang.Enum
and gain all of its nice features- Automatic singleton behaviour through correct serialization
- Automatic human-readable
.toString
method on enum values without the need to duplicate your enum names .name
and.ordinal
special-purpose methods- Usable in high-performance bitset-based
EnumSet
andEnumMap
classes
- Enums are treated by the language specially:
- Enums use a special syntax which simplifies instance creation without writing dozens of
public static final
fields - Enums can be used in
switch
statements - Enums cannot be instantiated outside the enumeration list except by using reflection
- Enums cannot be extended outside the enumeration list
- Enums use a special syntax which simplifies instance creation without writing dozens of
- Java automatically compiles extra stuff into enums:
public static (Enum)[] values();
public static (Enum) valueOf(java.lang.String);
private static final (Enum)[] $VALUES;
(values()
returns a clone of this)
Most of these can be emulated with a suitably designed class, but Enum
just makes it really easy to create a class with this set of particularly desirable properties.