//Execution order: (Priority from high to low.)
Static code block > mian method > construct code block > construct method.
The static code block is executed only once. The constructed code block is executed every time the object is created.
1. Ordinary code blocks
public static void main(String[] args) {/*Ordinary code block: * Directly defined in the method or statement "{Ordinary code execution statement}" appears in the method or statement, it is called the ordinary code block. *The order of execution of ordinary code blocks is determined by the order in which they appear in the code - "first appear first" */{ System.out.println("Here is ordinary code block A");}//new A();{ System.out.println("Here is ordinary code block B");}} Execution result: Here is the normal code block A
Here is the normal code block B
2. Static code blocks and construction code blocks
Blocks of code declared using static keyword in java.
It is often used to initialize classes. Each static code block will be executed only once (the class is executed when loading in memory, and the class already exists after the class is loaded in memory). Since the JVM will execute static code blocks when loading the class, the static code block will be executed before the main method. If the class contains multiple static code blocks, it will be executed first by "the defined code first, and then the defined code will be executed later.
ps:
1 Static code blocks cannot exist in any method body.
2 Static code blocks cannot directly access static instance variables and instance methods, and need to be accessed through the instance object of the class.
Construction block: A code block that is directly defined in the class and does not have a static keyword is called the {} construction code block.
The constructor code block is called when creating an object, and each time the object is created, it is called, and the execution order of the constructor code block takes precedence over the class constructor.
public class structure {{System.out.println("Here is a normal code block"); // There is a default constructor in all classes. The code block here is a constructor code block, which is executed when the object in the class is created}public static void main(String[] args) {/*Ordinary code block: * Directly defined in the method or statement "{execution statement of ordinary code}" appears in the method or statement, it is called ordinary code block. *The order of execution of ordinary code blocks is determined by the order in which they appear in the code - "first appear first" */{System.out.println("Here is ordinary code block A");}new structure();//The static code block does not execute when the second class loads //new A();{System.out.println("Here is ordinary code block B");}}static{System.out.println("Here is static code block");}} Execution results:
Here is the static code block //Preferential to the main function Here is the normal code block A
Here is a normal code block // object in the class is executed when it is created. Each time it is created, the new structure() is added; the execution result is:
Here is the normal code block B
3. Summary
public class structure {{System.out.println("Here is a normal code block");}public static void main(String[] args) {{System.out.println("Here is a normal code block A");}//new structure();//new structure();new A();{System.out.println("Here is a normal code block B");}}static{System.out.println("Here is a static code block");}}class A{static{System.out.println("Here is normal static code block 1 in A");}{System.out.println("Here is normal code block 1 in A");}{System.out.println("Here is normal code block 2 in A");}} Execution results:
Here is the static code block Here is the normal code block A
Here is the normal static code block 1 in A
Here is the normal code block 1 in A
Here is the normal code block 2 in A
Here is the normal code block B
Priority summary: Static code block > Main() > Construct code block