permaculture playing cards*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums in 1.4" Watch "Enums in 1.4" New topic
Author

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.

Thanks,
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

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
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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,
thanks
devan


->R§H<-
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Oct 13, 2005
Posts: 37941
    
  22
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Enums in 1.4
 
Similar Threads
Where do enums live?
Enum Initializers
Let me put it this way......
How to define constants for several classes?
Use of Enums over Interface constants ?