Home >Java >javaTutorial >Give an example of the execution order of code blocks in Java
Foreword
Today when I was looking at the implementation of Android ContentProvider, I suddenly thought of the execution order of static fields, static blocks, non-static fields, non-static blocks, and constructors in the Java class during the new process. In fact, this is a very classic question, which tests the mastery of basic knowledge of Java. I believe there are many questions like this during the interview process, so take advantage of the time on the weekend to review them.
Conclusion
Here I will give you the compiled conclusion first, and then I will write a program to verify our conclusion. When a Java class is new, the execution sequence is as follows:
Implement its own static attributes and static code blocks. (Determine who executes first based on the order in which the code appears)
Implement your own non-static properties and non-static code blocks.
Execute its own constructor.
In the process of implementing the inherited class to be new, the initialization execution sequence is as follows:
Implement the public static properties and static block-level code of the parent class.
Implement your own static attributes and static block-level code.
Implement the non-static properties and non-static code blocks of the parent class.
Execute the constructor of the parent class.
Implement your own non-static properties and non-static code blocks.
Execute its own constructor.
Here we need to briefly introduce static code blocks and non-static code blocks.
1. Static code block:
static {
}
2. Non-static code block
{
}
The similarities and differences between static code block and non-static code block are as follows:
Same points: both are J VM It is executed when the class is loaded and before the constructor is executed. Multiple variables can be defined in the class. Generally, some static variables are assigned values in the code block.
Difference: Static code block is executed before non-static code block (static code block > non-static code block). The static code block is only executed once when it is new for the first time, and will not be executed after that. Non-static code blocks are executed every time they are new.
Verification
The best verification of the conclusion is to write the code to prove the results. First, let’s take a look at the execution sequence when initializing a class without inheritance. The code is as follows:
public class InitOderTest { public static String STATIC_FIELD = "静态属性"; // 静态块 static { System.out.println(STATIC_FIELD); System.out.println("静态代码块"); } public String field = "非静态属性"; // 非静态块 { System.out.println(field); System.out.println("非静态代码块"); } public InitOderTest() { System.out.println("无参构造函数"); } public static void main(String[] args) { InitOderTest test = new InitOderTest(); } }
Execution results:
Static properties
Static code blocks
Non-static properties
Non-static code blocks
No-argument construction Function
Next, let’s verify whether the execution order is consistent with our conclusion when the Java class implements inheritance. The test code is as follows:
class ParentTest { public static String PARENT_STATIC_FIELD = "父类-静态属性"; // 父类-静态块 static { System.out.println(PARENT_STATIC_FIELD); System.out.println("父类-静态代码块"); } public static String parentField = "父类-非静态属性"; // 父类-非静态块 { System.out.println(parentField); System.out.println("父类-非静态代码块"); } public ParentTest() { System.out.println("父类—无参构造函数"); } } public class InitOderTest extends ParentTest { public static String STATIC_FIELD = "静态属性"; // 静态块 static { System.out.println(STATIC_FIELD); System.out.println("静态代码块"); } public String field = "非静态属性"; // 非静态块 { System.out.println(field); System.out.println("非静态代码块"); } public InitOderTest() { System.out.println("无参构造函数"); } public static void main(String[] args) { InitOderTest test = new InitOderTest(); } }
The execution results are as follows:
Parent class-static properties
Parent class-static code block
Static properties
Static code block
Parent class-non-static properties
Parent class-non-static Code block
Parent class—no-argument constructor
Non-static properties
Non-static code block
No-argument constructor
For more examples of the execution order of code blocks in Java, please pay attention to the PHP Chinese website for related articles!