accessing static constant does not initialize class

Discussion in 'Java' started by Tarlika Elisabeth Schmitz, Dec 11, 2004.

  1. 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
    Tarlika Elisabeth Schmitz, Dec 11, 2004
    #1
    1. Advertising

  2. Tarlika Elisabeth Schmitz

    Ryan Stewart Guest

    "Tarlika Elisabeth Schmitz" <> wrote in message
    news:...
    [...]
    > 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."
    Ryan Stewart, Dec 11, 2004
    #2
    1. Advertising

  3. Hi Ryan,

    Ryan Stewart wrote:

    > "Tarlika Elisabeth Schmitz" <> wrote in message
    > news:...


    >>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
    Tarlika Elisabeth Schmitz, Dec 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    562
    Chris Uppal
    Nov 17, 2005
  2. RCAJ
    Replies:
    6
    Views:
    40,566
    Jeff Schwab
    Dec 28, 2003
  3. lou zion
    Replies:
    8
    Views:
    6,005
    Howard
    Jun 9, 2005
  4. Alan Johnson
    Replies:
    1
    Views:
    397
    Alf P. Steinbach
    May 11, 2006
  5. Robin
    Replies:
    0
    Views:
    404
    Robin
    Jun 6, 2007
Loading...

Share This Page