T
Tarlika Elisabeth Schmitz
Hello,
The following code totally flummoxed me:
public class Test
{
public static void main(String args[])
{
System.out.println ("testing fixed string "+ A.AA);
System.out.println ("testing string "+ A.AAA);
}
}
class A
{
public static final String AA= "AA";
public static final String AAA= String.valueOf('a');
static
{
System.out.println ("Init superclass A");
}
}
Before you read on: what do you think should the output be?
Output:
-------
testing fixed string AA
Init superclass A
testing string a
In other words: Accessing a static constant does not cause the static
initializer to be executed. Only when the computed static String is
accessed is class A initialized. Also, when calling a static method the
initializer gets executed.
Is this a feature or a bug?!
My understanding was that accessing a static variable, invoking a static
method or instantiating a new object should trigger execution of the
static initializer.
I am testing under Windoze, Java 1.4.2_06. Same behavour on Linux,
Blackdown 1.4.2. But then again, Blackdown is pretty much a Sun clone.
--
Regards/Gruß,
Tarlika Elisabeth Schmitz
The following code totally flummoxed me:
public class Test
{
public static void main(String args[])
{
System.out.println ("testing fixed string "+ A.AA);
System.out.println ("testing string "+ A.AAA);
}
}
class A
{
public static final String AA= "AA";
public static final String AAA= String.valueOf('a');
static
{
System.out.println ("Init superclass A");
}
}
Before you read on: what do you think should the output be?
Output:
-------
testing fixed string AA
Init superclass A
testing string a
In other words: Accessing a static constant does not cause the static
initializer to be executed. Only when the computed static String is
accessed is class A initialized. Also, when calling a static method the
initializer gets executed.
Is this a feature or a bug?!
My understanding was that accessing a static variable, invoking a static
method or instantiating a new object should trigger execution of the
static initializer.
I am testing under Windoze, Java 1.4.2_06. Same behavour on Linux,
Blackdown 1.4.2. But then again, Blackdown is pretty much a Sun clone.
--
Regards/Gruß,
Tarlika Elisabeth Schmitz