This discussion is archived
2 Replies Latest reply: Jul 27, 2013 2:39 PM by rp0428 RSS

Why global var can be initialized with a static method and not by other static global var declared after its usage

abroshan-Oracle Explorer
Currently Being Moderated

Take this:

class test

{

static int i=j;

static int j=10;

.....

 

this will give illegal forward reference ....

 

but this will compile successfully ..

 

class test

{

static int i=test1();

static test1()

{

return 20;

}

}

.....

 

plz assume we have main method in both cases ..

 

java would be loading all static members first and would be assigning default values .. and then will be running all the initializers from to bottom ..

Why second case is a compile success and not first .. as in second also test1 method is declared after its usage ..

 

Plz help.

 

Thanks

Abhishek Roshan

  • 1. Re: Why global var can be initialized with a static method and not by other static global var declared after its usage
    abroshan-Oracle Explorer
    Currently Being Moderated

    Okay ... I think when the java would be executing the initilazers from top to bottom , in second case when it would be doing so , it first has to run the test1() method so so i gets its value ..

    but in first case  when the java would be executing the initializers , it doesn't need to run j as such as j is a literal ..but then java would already have first saved all the static members values before executing initializers .. so then i shoud have got the j value which was save to 0 by default.

    Plz help me in clearing this concept.

    thanks

    abhishek roshan

  • 2. Re: Why global var can be initialized with a static method and not by other static global var declared after its usage
    rp0428 Guru
    Currently Being Moderated

    Why second case is a compile success and not first .. as in second also test1 method is declared after its usage ..

    Because the implementors of Java intentionally chose to do it that way.

     

    There are TWO stages to the process: preparation (which occurs first) and initialization.

     

    See the Java Language Spec section 12.4.1 'When Initialization Occurs

     

     

    The intent is that a class or interface type has a set of initializers that put it in a consistent state, and that this state is the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

    Note the clause beginning 'may not refer to class variables'. And the authors give the reason for that restriction in the last sentence: detect circular initializations.

     

    Then if you check that referenced section 8.3.2.3 you will find this

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3

     

    8.3.2.3. Restrictions on the use of Fields during Initialization

    The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

    •   The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
    •   The usage is not on the left hand side of an assignment.
    •   The usage is via a simple name.
    •   C is the innermost class or interface enclosing the usage.

    When a method is used (your example 2) no circular initialization can occur because methods are not 'initialized'.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points