File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Strange Enum behaviour? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strange Enum behaviour?" Watch "Strange Enum behaviour?" New topic

Strange Enum behaviour?

Ec Kjellberg

Joined: Aug 12, 2004
Posts: 9
What happens in this (rather stupid) sample?
It looks like the static variable 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:

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
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

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.

Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

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 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:
[ August 15, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Or move the value initializer into a user-defined static block after the next variable is defined. An example:
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Of course in this particular example, an alternative to get the needed behavior could be this:

Or this:

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Strange Enum behaviour?
It's not a secret anymore!