Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enums in 1.4

 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Devan Brahma
Ranch Hand
Posts: 46
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You apparently haven't read my year old post, since it explains exactly how to do this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply dudes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic