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.
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++;
}
}
decompiles as:
public final class Trees extends Enum
{
public static final Trees[] values()
{
return (Trees[])$VALUES.clone();
}
public static Trees valueOf(String s)
{
return (Trees)Enum.valueOf(Trees, s);
}
private Trees(String s, int i, boolean flag)
{
super(s, i);
counter(flag);
}
static void counter(boolean flag)
{
if(flag)
coniferousCount++;
}
public static final Trees PINE;
public static final Trees ASPEN;
private static int coniferousCount;
private static final Trees $VALUES[];
static
{
PINE = new Trees("PINE", 0, true);
ASPEN = new Trees("ASPEN", 1, false);
$VALUES = (new Trees[] {
PINE, ASPEN
});
}
}
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.