An enum mystery solved

I was baffled why the compiler would not let enum constructors access
the enum's static variables.

I consider it a bug, but an understandable bug.

I think the reason is the enum invokes the constructors for the enum
constants in static init code. Somebody was worried that the static
initialisation would not be complete. Yet it is quite safe since the
enum constants are the last bit of static init.

Advertisements

On Fri, 26 Aug 2005 05:58:23 GMT, Roedy Green
<> wrote or quoted :
>I think the reason is the enum invokes the constructors for the enum
>constants in static init code. Somebody was worried that the static
>initialisation would not be complete. Yet it is quite safe since the
>enum constants are the last bit of static init.

I have placed an RFE with Sun to have the restriction repealed. In the
meantime, oddly you can call static METHODS that access the static
variables.

Advertisements

> I was baffled why the compiler would not let enum constructors access
> the enum's static variables.

If I understand the issue correctly, jls3 8.9 addresses this issue: It
is compile-time error to reference a non-constant (§15.28) static
field of an enum type from its constructors, instance initializer
blocks, or instance variable initializer expressions.
>
> I consider it a bug, but an understandable bug.
>
> I think the reason is the enum invokes the constructors for the enum
> constants in static init code. Somebody was worried that the static
> initialisation would not be complete. Yet it is quite safe since the
> enum constants are the last bit of static init.
>
A small test seems to indicate that enum constants are the /first/ bit
of static init and that the static final variables are not initialized
during this enum constant construction.

Hemal Pandya wrote:
>
> A small test seems to indicate that enum constants are the /first/ bit
> of static init and that the static final variables are not initialized
> during this enum constant construction.

I think what Roedy missed was the distinction between a compile time
constant and other static finals. A compile time constant will be
inlined and therefore there is no problem. A static final initialised
within static { }/<clinit> will cause a problem.

> In the
> meantime, oddly you can call static METHODS that access the static
> variables.

This seems analogous to the "illegal forward reference" warning when
one static final variable is refers to another static final (but not
constant) variable but can get NullPointerException if the same forward
reference is indirectly achieved using a static method.

On 26 Aug 2005 03:27:04 -0700, "Hemal Pandya" <>
wrote or quoted :
>A small test seems to indicate that enum constants are the /first/ bit
>of static init and that the static final variables are not initialized
>during this enum constant construction.

There is something deeply wrong when Java starts adding arbitrary
restrictions like that making no sense in the high level language.

I decompiled and discovered the construction of the enum constants
came last. What is your code that the inits come after the
construction? Why could not the compiler put it before and be done
with this silly restriction, simultaneously making it safer to use
static methods.

You can see that had the i=x been allowed, it should have been fine.
The constructors are run after the x = Math.min(2,1) are they not?
--
Canadian Mind Products, Roedy Green.http://mindprod.com Again taking new Java programming contracts.

Roedy Green wrote:
> On 26 Aug 2005 03:27:04 -0700, "Hemal Pandya" <>
> wrote or quoted :
>
>
>>A small test seems to indicate that enum constants are the /first/ bit
>>of static init and that the static final variables are not initialized
>>during this enum constant construction.
>
>
> There is something deeply wrong when Java starts adding arbitrary
> restrictions like that making no sense in the high level language.

Initialisation has got to be done in some order. Doing it in the order
presented in the source code seems like the method of least surprises to me.
> I decompiled and discovered the construction of the enum constants
> came last. What is your code that the inits come after the
> construction? Why could not the compiler put it before and be done
> with this silly restriction, simultaneously making it safer to use
> static methods.
>
> here is an example:
>
> public enum Trees
> {
> PINE( true ),
> ASPEN ( false );
>
> private static int coniferousCount;
>
> Trees( boolean coniferous )
> {
> counter( coniferous );
> }
>
> static void counter( boolean coniferous )
> {
> if ( coniferous ) coniferousCount++;
> }
>
> }

This is an irrelevant example. It does not do any user static
initialisation.

Here's my example:

enum Order {
X;
private static final long time = System.currentTimeMillis();
}

Roedy Green wrote:
>
> Here is your code modified slightly to make it compile:
> [...]
> Here is how it decompiles:
> [...]
> You can see that had the i=x been allowed, it should have been fine.
> The constructors are run after the x = Math.min(2,1) are they not?

I don't think you should depend upon your decompiler being accurate.
javap -c is the way to go. You can see that the actual byte code is
different from what your decompiler is claiming.

Roedy Green wrote:
>
> what term do you use to distinguish been a constant like this:
>
> private static final int X = Math.min(2,1);
>
> and like this:
>
> private static final int Y = 42;
>
>
> The second is known at compile time, can be used in case labels, is
> in-lineable.

The first is really just a static final, colloquially known as a
constant. The latter is a "compile-time constant expression" according
to the JLS 3rd Ed, 15.28, p525. I believe the second edition has some
errata in that section about the use (or not) of nulls.

On Fri, 26 Aug 2005 20:39:17 +0100, Thomas Hawtin
<> wrote or quoted :
>I don't think you should depend upon your decompiler being accurate.
>javap -c is the way to go. You can see that the actual byte code is
>different from what your decompiler is claiming.

You are such a clever character. You are right yet again. The
compiler indeed does the static field inits AFTER the constructor
calls. If it reversed the order, the way DJ decompiles it, Java would
have no problem with static references in constructors.

Here is the Javap decompilation of Final.java

javap -c Final

Compiled from "Final.java"
final class Final extends java.lang.Enum{
public static final Final X;

Roedy Green wrote:
> On Fri, 26 Aug 2005 20:39:17 +0100, Thomas Hawtin
> <> wrote or quoted :
>
>
>>I don't think you should depend upon your decompiler being accurate.
>>javap -c is the way to go. You can see that the actual byte code is
>>different from what your decompiler is claiming.
>
>
> You are such a clever character. You are right yet again. The
> compiler indeed does the static field inits AFTER the constructor
> calls. If it reversed the order, the way DJ decompiles it, Java would
> have no problem with static references in constructors.
>

But there would be problems with static references to the enum values,
don't you think? So this may be a damned if do you, damned if you don't
type situation.

On Sat, 27 Aug 2005 01:40:26 GMT, Raymond DeCampo
<> wrote or quoted :
>But there would be problems with static references to the enum values,
>don't you think? So this may be a damned if do you, damned if you don't
>type situation.
Ah ha!

To solve that one you would need some sort of spread-sheet like
natural ordering of the initialisations which would be very
unJava-like.

But then you'd think you could SET static values in constructors, just
not reference them. Perhaps that is how it does work.

I also want to recheck what their thinking is on compile time static
constants.

I suppose you get into similar situation if you have a pair of classes
whose static inits cross reference each other. It is just that Java
does not warn you of the problems.
--
Canadian Mind Products, Roedy Green.http://mindprod.com Again taking new Java programming contracts.

On Fri, 26 Aug 2005 20:51:14 +0100, Thomas Hawtin
<> wrote or quoted :
>constant. The latter is a "compile-time constant expression" according
>to the JLS 3rd Ed, 15.28, p525. I believe the second edition has some
>errata in that section about the use (or not) of nulls.

I have written an essay on what I have discovered about constants.
Other that the usual error checking, I invite you to submit some
counter-intuitive examples on what is compile-time and what is a
load-time constant.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!