File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Enums in 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums in 1.4" Watch "Enums in 1.4" New topic

Enums in 1.4

Craig Taylor
Ranch Hand

Joined: Jul 17, 2008
Posts: 64
After checking code into a shared library one of the other developers says, "It needs to be 1.4 compliant."

Fine, I go through change all the simple For( x : someList ) to work off iterators and other minor annoyances yet my Enum classes I'm wanting to keep.

What is the common convention to handle Enum's or enum like values in java 1.4? I'd like to avoid static final as .... well, they're just ugly.

Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

The Sun enum guide briefly addresses this issue and refers to the Typesafe Enum pattern as a possible solution. If you can get your hands on the first edition of Effective Java, there's tip in there that explain how to implement this. If you can't, a Google search should turn up some usefull results.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Given the following enum:

The following comes closest to recreating these:

That does leave out values, valueOf and getDeclaringClass, but those shouldn't be too hard.

In fact, I've compiled my Card enum and decompiled it, and this is the actual result:

If you copy Sun's Enum class, all that is a problem is valueOf - it uses Class.enumConstantDirectory(). Although you can implement valueOf for the enum itself without too much problems:

That only leaves serialization (hint: search for readResolve) and the switch statement.
[ September 26, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
Devan Brahma
Ranch Hand

Joined: Jul 07, 2009
Posts: 46

hai ,
i have a problem here that is i have a enum class like this

and so on...
now how can i declare that in jdk 1.4,

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

You apparently haven't read my year old post, since it explains exactly how to do this.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Presumably in this case you can use the ordinal field as an array in that valueOf() method, and indirectly for the switch.
Devan Brahma
Ranch Hand

Joined: Jul 07, 2009
Posts: 46

thanks for reply dudes
I agree. Here's the link:
subject: Enums in 1.4
It's not a secret anymore!