Associating enum values with ints

Simple things should be simple, complex things should be possible.
Alan Kay

I've been working quite a lot in Java. As part of the university course we work with BT's people and 'bring meaningful innovation'. Because of university policies, we have to work in Java. Every now and again (more often than not actually), I came across painfully annoying constructs which make the code that much harder to read and write. Like today, I had to work with enums. They are really simple in C#:

Meanwhile the same construct in Java:

Taken from Stack Overflow. Original answer by: adarshr on Jun 15 2012 at 9:15.

Obviously, enums in Java are far more sophisticated and powerful than in C#:

1) Type safety and value safety.
2) Guaranteed singleton.
3) Ability to define and override methods.
4) Ability to use values in switch statement case statements without qualification.
5) Built-in sequentialization of values via ordinal().
6) Serialization by name not by value, which offers a degree of future-proofing.
7) EnumSet and EnumMap classes.
EPJ on StackOverflow

Nevertheless, I am struggling to appreciate those benefits especially as they come at a heavy price of readability and maintainability of the code. For example, more often than not, Singletons are considered an anti-pattern. At the moment I am silently praying that this may be one of the remnants of the old Java - but who knows.

Leave Comment

Your email address will not be published.

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box