• 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

Strange Enum behaviour?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens in this (rather stupid) sample?
It looks like the static variable Enum.next is not initialised when the Enum are and therefore their member variable value is initialised with values from 0 to 2 (instead of 'a' to 'c' as one might think).

The output is at least:
a
0
1
2
a

And the code looks like:



I assume that this is a well defined initialisation order behind this but nevertheless I was a little bit surprised.

If anyone care to elaborate a little bit and maybe explain a little I'd be very happy and maybe a little bit less confused.

Thanks // Ecka
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enum will be translated to a class file, with each enum constant being a static constant inside the enum class declaration.

Since an enum constant is a static field, it has to be constructed inside a static block. To avoid problems with user-declared static field referring to the enum constants the static block is executed before initialization of the static fields.

If I were to write your enum as a class definition it would look like this (leaving out the built-in enum code):


So the enum constants are constructed before the static field 'next' is initialized.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really, really want to, and you really, really shouldn't, you could declare an inner class inside the enum. The inner class can declare and initialize static fields before the enum constructor runs, and make them accessible to the constructor.



This modified version of your code sample will actually initialize the member value of Enum.A to char value 'a', Enum.B to char value 'b' and Enum.C to char value 'c', as soon as the Enum is used. The initial Enum.next() call will therefore yield char value 'd' and subsequent calls will continue to increment the value of the static field. So the output should read:
d
a
b
c
e
[ August 15, 2008: Message edited by: Jelle Klap ]
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or move the value initializer into a user-defined static block after the next variable is defined. An example:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course in this particular example, an alternative to get the needed behavior could be this:



Or this:

 
reply
    Bookmark Topic Watch Topic
  • New Topic