Advantages of Java’s enum over the old “Typesafe Enum” pattern?

  • “cannot be subclassed” isn’t a restriction. It’s one of the big advantages: It ensures there there is always only ever exactly the set of values defined in the enum and no more!
  • enum correctly handles serialization. You can do that with type-safe enums as well, but it’s often forgotten (or simply not known). This ensures that e1.equals(e2) always implies e1 == e2 for any two enum values e1 and e2 (and vice versa, which is probably more important).
  • There are specific lightweight data structures that handle enums: EnumSet and EnumMap (stolen from this answer)

Leave a Comment