From: Todd on
Hello,

I have recently been told that the ordinal() method in a Java enum
will not necessarily return the same value in different invocations of
the JVM. Has anyone else found this?

BTW, the semantics of the enum will stay the same, an enumerated value
that was once greater than another will continue to be so. It is just
that the absolute value of the declared enumeration will not
necessarily be the same from run to run. This indicates to me that
one would not be able to retrieve an enumerated element by a stored
ordinal value. Further, the ordinal could be a value greater than the
number of enumerated elements, making values()[ordinal()] suspect as
well.

Any insights are appreciated,
Todd
From: Mayeul on
Hello,

Todd wrote:
> I have recently been told that the ordinal() method in a Java enum
> will not necessarily return the same value in different invocations of
> the JVM. Has anyone else found this?

Never observed this, and it would be contradictory with the JavaDoc of
Enum.ordinal() . I often expect ordinal() to behave as specified, and
have never noticed this expectation to break anything.

> [...] This indicates to me that
> one would not be able to retrieve an enumerated element by a stored
> ordinal value.

Well that /is/ pretty unsafe to do anyway. What if you need to
add/remove/swap enum values?
I usually store their name instead, or try to think up some safe enough
serialization.

> [...]

--
Mayeul
From: Joshua Cranmer on
On 04/08/2010 12:13 PM, Todd wrote:
> I have recently been told that the ordinal() method in a Java enum
> will not necessarily return the same value in different invocations of
> the JVM. Has anyone else found this?

To do so would contradict the API:
public final int ordinal()

Returns the ordinal of this enumeration constant (its position in
its enum declaration, where the initial constant is assigned an ordinal
of zero). Most programmers will have no use for this method. It is
designed for use by sophisticated enum-based data structures, such as
EnumSet and EnumMap.

Returns:
the ordinal of this enumeration constant

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Todd on
On Apr 8, 11:43 am, Joshua Cranmer <Pidgeo...(a)verizon.invalid> wrote:
> On 04/08/2010 12:13 PM, Todd wrote:
>
> > I have recently been told that the ordinal() method in a Java enum
> > will not necessarily return the same value in different invocations of
> > the JVM.  Has anyone else found this?
>
> To do so would contradict the API:
> public final int ordinal()
>
>      Returns the ordinal of this enumeration constant (its position in
> its enum declaration, where the initial constant is assigned an ordinal
> of zero). Most programmers will have no use for this method. It is
> designed for use by sophisticated enum-based data structures, such as
> EnumSet and EnumMap.
>
>      Returns:
>          the ordinal of this enumeration constant
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

I fully agree. I was told that the JavaDocs were wrong. I tried
locating a source on the web to corroborate the assertion, but
couldn't find one.
From: John B. Matthews on
In article
<69c37aba-9511-49b4-924f-1ad0236b9ba7(a)z4g2000yqa.googlegroups.com>,
Todd <todd.heidenthal(a)gmail.com> wrote:

> On Apr 8, 11:43 am, Joshua Cranmer <Pidgeo...(a)verizon.invalid> wrote:
> > On 04/08/2010 12:13 PM, Todd wrote:
> >
> > > I have recently been told that the ordinal() method in a Java
> > > enum will not necessarily return the same value in different
> > > invocations of the JVM.  Has anyone else found this?
> >
> > To do so would contradict the API:
> > public final int ordinal()
> >
> >      Returns the ordinal of this enumeration constant (its position in
> > its enum declaration, where the initial constant is assigned an ordinal
> > of zero). Most programmers will have no use for this method. It is
> > designed for use by sophisticated enum-based data structures, such as
> > EnumSet and EnumMap.
> >
> >      Returns:
> >          the ordinal of this enumeration constant
> [...]
> I fully agree. I was told that the JavaDocs were wrong. I tried
> locating a source on the web to corroborate the assertion, but
> couldn't find one.

I'd have thought to avoid ordinal(), as suggested by Joshua Bloch,
"Item 31: Use instance fields instead of ordinals."

Regarding serialization, section 1.12 of "Java Object Serialization
Specification," entitled "Serialization of Enum Constants," mentions
using "the value returned by the enum constant's name method."

<http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>