This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic