-3

Below is a program in which there are two static block which in my opinion should be called at first notice but in order from top to bottom but that does not happen

public class StaticTest {  
    public static void main(String[] args) {  
        A a1 = new A();  
        A a2 = new A();  
        B b1 = new B();  
    }
}  

class A {  
    static {  
        System.out.println("Static block of Class A");  
    }  
    {  
        System.out.println("Non-Static block of a instance of Class A");  
    }  
    public A() {  
        System.out.println("Constructing object of type A");  
    }  
}  
class B {  
    static {  
        System.out.println("Static block of Class B");  
    }
    {  
        System.out.println("Non-Static block of a instance of Class B");  
    }
    public B() {  
        System.out.println("Constructing object of type A");  
    }  
}   
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
Monis
  • 167
  • 2
  • 13

5 Answers5

3

"Exactly at what time static block or method is invoked" - For question like this, the Java Language Specification has the answer for you.

Static initializer:

A static initializer declared in a class is executed when the class is initialized. Together with any field initializers for class variables, static initializers may be used to initialize the class variables of the class.

By running your code, you can easily figure out what runs first.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

The static block in class B is not executed until B is actually needed. This is only the case in the 3rd statement of the main method. In fact, if you replace that line with

if (2*2 == 5) new B();

you'll notice that the static block of B is never executed, because B will not even be loaded.

To trace class loading, use the -verbose:class option of the java command.

Ingo
  • 36,037
  • 5
  • 53
  • 100
1

This is the output of your test

Static block of Class A
Non-Static block of a instance of Class A
Constructing object of type A
Non-Static block of a instance of Class A
Constructing object of type A
Static block of Class B
Non-Static block of a instance of Class B
Constructing object of type A

I guss you are surprised why Static block of Class A is printed only once. This is because the static block runs only once on class loading. After you called new A() JVM loaded A.class and when you called new A() again there was no need to load it again

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

The static blocks are executed when the respective classes are initialized.

The order of class initialization is determined by a combination of the static and dynamic dependencies between the classes involved; see When does static class initialization happen?

However, the JLS is effectively providing a set of constraints (this occurs before that) for which there are often many solutions. And this is an example where two "solutions" to the constraints are possible.

  1. The class A is initialized before the new A() expression constructs an A

  2. The class B is initialized before the new B() expression constructs an B

As you can see, initialize class A before B and B before A are both solutions.

In a situation like this, the actual order of initialization is not specified, and is likely to be JVM and/or bytecode compiler dependent ... and should not be relied upon.

It is also possible to construct a scenario in which there is a cycle in the dependencies, which results in there being no order that satisfies the constraints. In this case, the order is also indeterminate, and one of the classes may observe the other class in an uninitialized state. (Neither the compiler or the runtime flags this as an error!) This is described in JLS section 8.3.2.3.

Fortunately, this situation is pretty unusual.


Instance blocks and static methods are much simpler:

  • The instance blocks of a class are executed in program order each time an instance is created. They are executed in order between a constructor's super(...) "call" and the next line of that constructor. (The same applies when the constructor is the default constructor and/or the super() call is implicit.)

  • A static method is executed when it is explicitly called.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

following is the output of your program

Static block of Class A
Non-Static block of a instance of Class A
Constructing object of type A
Non-Static block of a instance of Class A
Constructing object of type A
Static block of Class B
Non-Static block of a instance of Class B
Constructing object of type A

Static block is called only once when first time class object is created

fvu
  • 32,488
  • 6
  • 61
  • 79