Static Initialization Lock Order

I found a lot of posts about static initialization blocks, however I am trying to get a little better idea of โ€‹โ€‹the execution order and reason. The code below prints text in both static blocks and then prints text in the main static block.

I understand how the compiler calls it to make all the static blocks when the class is loaded, and then access the main method. But since the main method is static in itself, why not execute it in the order of other static blocks (not even sure if it is useful, just trying to understand the concept, and if there is an urgent reason for doing this this way). What if there is a static block that we want to run after the main block?

class Cat { static { System.out.println("This block welcomes you first"); } public static void main(String[] args) { System.out.println("Meow world "); } static { System.out.println("This block welcomes you after"); } } 

Actual output

 This block welcomes you first This block welcomes you after Meow world 

Why not?

 This block welcomes you first Meow world This block welcomes you after 
+5
source share
3 answers

Static initializers are executed as soon as the class is loaded. The main method is called after loading the class.

This JLS section discusses the sequence of events ( 12.1.3-4 ):

12.1.3. Initialize Test: Run Initializers

In our ongoing example, the Java virtual machine is still trying to execute the main method of the Test class. This is only allowed if the class has been initialized (ยง12.4.1).

Initialization consists of executing any initializers of class variables and static initializers of class Test in textual order. But before Test can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, etc., recursively. In the simplest case, Test has an object as its implicit direct superclass; if the Object class has not yet been initialized, it must be initialized before the test is initialized. The class object does not have a superclass, so recursion ends here.


12.1.4. Invoke Test.main

Finally, after initialization is complete for the Test class (during which other subsequent loading, binding, and initialization could occur), the main test method is called.

+7
source

The runtime system ensures that static initialization blocks are called in the order they appear in the source code. And don't forget that this code will be executed when the JVM loads the class. The JVM combines all of these blocks into one static block and then executes. Here are a few points that I would like to mention:

 If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM. If you're referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM same as above ie, now the static variables/methods referred and the static block both will be executed. 

After that, the main method will be executed. Thanks

+6
source

Static initializers are executed as soon as the class is initialized (the class can be loaded and initialized at a later point in time. Class.forName () ). The main() method is called after the static initializers are executed. Thus, the way out.

+2
source

Source: https://habr.com/ru/post/1209812/


All Articles