wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java 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 "Java "enum" : can you use non-consecutive ordinal numbers ?" Watch "Java "enum" : can you use non-consecutive ordinal numbers ?" New topic
Author

Java "enum" : can you use non-consecutive ordinal numbers ?

Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Hi,

When using java "enum" , can I ask java to assign my own specific ordinals, instead of the default consecuitive values ?

For example, say I have the following ACTION codes (I can't control the code
values because they're taken from some legacy system):
* DELETE = 2;
* ADD = 5;
* EDIT =7;

I couldn't find any 'enum' tutorial which deals with it...
I could only find examples in the lines of:


Any way to get the correct codes ?
Thanks !
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I haven't used Java enums much, but I don't think you can assign your own ordinals to enum constants.

However, enums in Java are quite flexible, they behave as a special kind of class. You could do this, for example:

Then you can use for example DELETE.getCode() to get the value 2 which should be associated with the DELETE enum value.
[ September 19, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Thanks very much
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Jesper Young:]I haven't used Java enums much, but I don't think you can assign your own ordinals to enum constants.

Nor did I, and never really got the hang of it.

I just got used to static final, but your response may deprive the poster of needed design avenues:



At: Enums on the Java server.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Thanks very much, Nicholas Jordan, for your reply.

But could I please ask what's the difference between the design approach in your example (e.g. MERCURY (3.303e+23, 2.4397e6)) and the design approach in Jesper Young's reply (e.g. DELETE(2)) ?
I mean, don't they both reflect the idea of adding fields to your enum, and initializing them in the private constructor ?

Or did you mean to emphasize that additional fields can be 'double' and not just limited to 'int' ?

Thanks very much
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I meant tp emphasize that additional fields can be added and other things that may be overlooked should be investigated before making further design decisions. I never really got going on enums when they seemed to be critical path selectors placed early in executable files where the runtime, in other languages, would switch on two byte or one byte values early in the loadable program that we in userland can see as a filename in a directory listing under the explorer interface.

Therefore, I just quit using them.

I was trying to draw attention that the decision needed further attention, nothing else beyond that.

Message edit: see the discussion I wanted to acheive by visiting the link:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

right next to the smiley.
[ September 23, 2007: Message edited by: Nicholas Jordan ]
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Thanks for this clarification.
And extra thanks for taking the time to reply in such detail

In my case, Jesper Young's reply did the job just great, just what I've been looking for.
But I agree that in general, it's very important to investigate as many options and alternatives as possible.
 
wood burning stoves
 
subject: Java "enum" : can you use non-consecutive ordinal numbers ?
 
Similar Threads
Enums in a map
compare using enum and int
"casting" an int to an enum type
Start java enum ordinal at 1 instead of 0
java enums and the database