R
Roedy Green
According to Joshua Bloch, the new enums have the following
properties:
They provide compile-time type safety--int enums don't provide any
type safety at all.
They provide a proper name space for the enumerated type--with int
enums you have to prefix the constants to get any semblance of a name
space.
They're robust--int enums are compiled into clients, and you have to
recompile clients if you add, remove, or reorder constants.
Printed values are informative--if you print an int enum you just see
a number.
Because they're objects, you can put them in collections.
Because they're essentially classes, you can add arbitrary fields and
methods.
This is BETTER than I hoped for. I thought for sure Sun would
sacrifice the ability to use the new enums in case clauses, making
them useless for much of the stuff I do.
What puzzles me is how did they pull this off. Sun went to great
lengths not to change the JVM to do generics. What compiles the
lookupswitch and tableswitch byte code at run time from the latest
enum definition? Enums must somehow look like ordinary static
constants. Yet they are not resolved at compile time.
properties:
They provide compile-time type safety--int enums don't provide any
type safety at all.
They provide a proper name space for the enumerated type--with int
enums you have to prefix the constants to get any semblance of a name
space.
They're robust--int enums are compiled into clients, and you have to
recompile clients if you add, remove, or reorder constants.
Printed values are informative--if you print an int enum you just see
a number.
Because they're objects, you can put them in collections.
Because they're essentially classes, you can add arbitrary fields and
methods.
This is BETTER than I hoped for. I thought for sure Sun would
sacrifice the ability to use the new enums in case clauses, making
them useless for much of the stuff I do.
What puzzles me is how did they pull this off. Sun went to great
lengths not to change the JVM to do generics. What compiles the
lookupswitch and tableswitch byte code at run time from the latest
enum definition? Enums must somehow look like ordinary static
constants. Yet they are not resolved at compile time.