accessing static constant does not initialize class

  • Thread starter Tarlika Elisabeth Schmitz
  • Start date
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
 
R

Ryan Stewart

[...]
In other words: Accessing a static constant does not cause the static
initializer to be 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.
[...]

JLS 2.0 §12.4.1--
"A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
. T is a class and an instance of T is created.
. T is a class and a static method declared by T is invoked.
. A static field declared by T is assigned.
. A static field declared by T is used and the reference to the field is
not a compile-time constant (§15.28). References to compile-time constants
must be resolved at compile time to a copy of the compile-time constant
value, so uses of such a field never cause initialization."
 
T

Tarlika Elisabeth Schmitz

Hi Ryan,

Ryan said:
My understanding was that accessing a static variable, invoking a static
method or instantiating a new object should trigger execution of the
static initializer.

[...]

JLS 2.0 §12.4.1--
not a compile-time constant (§15.28). References to compile-time constants
must be resolved at compile time to a copy of the compile-time constant
value, so uses of such a field never cause initialization."

Thanks

--


Regards/Gruß,

Tarlika
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top