0

Suppose JVM has loaded the following Foo class for the first time and it's just passed to Linking step ( during resolution ).

public static void main(...) {
     new Foo();
}

public class Foo {

     private static int a = 4;
     private static Hoo hoo = new Hoo();

     Foo (){
         Log.i("Test", ">> Test foo's created!");
     }

}

public class Hoo {

     private static int b = 4;
     private static Soo soo = new Soo();

     Hoo (){
         Log.i("Test", ">> Test hoo's created!");
     }

}

public class Soo {

     private static int b = 4;
     private static Koo soo = new Koo();

     Soo (){
         Log.i("Test", ">> Test soo's created!");
     }

}

...

Now my question is this. We know that prior to completing child class construction, system calls recursively base/super class constructor(s). Does there exist some similar logic lies in the above code? I mean does JVM will complete the construction of Foo right after completing the Hoo() and Soo() construction ?

Thx.

2
  • 4
    What does your log tell you? Commented Mar 29, 2016 at 15:02
  • Definetly tells what I think. But I wanted make myself sure if some tricky or exceptional stuff might occur during this recursion. Commented Mar 30, 2016 at 16:32

1 Answer 1

3

I mean does JVM will complete the construction of Foo right after completing the Hoo() and Soo() construction ?

Basically ... yes.

According to JLS 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.

  • 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 field is not a constant variable (§4.12.4).

  • T is a top level class (§7.6) and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.

and so on.

In your example, this means that the classes will be initialized in the order Koo, Soo, Hoo, Foo and that the executing new Foo() expression in the main method will trigger this.

Things get a bit complicated (and nasty) if there are cyclic dependencies between the static initialization of different classes, but the JLS covers this edge case too.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Can you a bit elaborate what might happen when there're cyclic dependencies ? Having null references for instance ?
Broad brush: code running as part of one classes initialization may see another classes statics in a partially initialized state. What that means depends on the situation.
One additional note: Of course it may vary but in our case, they're loaded in the order they appeared but linked and initialized as you wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.