Java 6 was recently released and I am still learning new Java 5 features in detail. I was recently presented with a situation where I thought the new Java 5 Enums would come in handy. I had a particular list of "types" that weren't going to be persisted in a reference table in the database because "...they will never change..." (I am waiting for them to change). At first I had written them as constants belonging to the domain object they relate to. I was glancing through Bruce Eckel's Thinking in Java 4th Edition and stumbled on the Enum chapter. I decided to try them out with this use case.
I'm not 100% sold on them as replacements for the static constant, yet. At least not in my scenario. But they are very handy and worth investigating more. Since they are actual objects in Java and not just int's as in C/C++ it makes them a bit more OO and since they are immutable like String, checking for equality is very simple. Also, since they are typesafe, it makes it impossible to pass an invalid parameter to a method expecting an enum value. This was the main reason I wanted to use them in my case.




