• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic