Java Notes From My Desktop

Tags - Categories : All | Articles | Book Reviews | CSS | Java | Javaranch | Javascript | News | Opengl | Opinions | Personal | php

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.




Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 6 + 5 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).