When we look at the implementation of Android ContentProvider today, I suddenly think of the Java class in the process of new, static domain, static block, non-static domain, non-static block, constructor execution order problem. In fact, this is a very classic question, very much to examine the Java basic knowledge of the degree of mastery. A lot of the interview process is believed to have such a problem, while the weekend has time to review.
Conclusion Let's put the finished conclusion to everyone, and then I'm writing a program to verify our conclusion. During the Java class being new, the order of execution is as follows:
- Implement its own static properties and static blocks of code. (determines who executes first, based on the order in which the code appears)
- Implement its own non-static and non-static blocks of code.
- Executes its own constructor.
In the process of implementing the inherited class being new, the initialization execution order is as follows:
- Implements the public static properties and static block-level code for the parent class.
- Implement its own static properties and static block-level code.
- Implements non-static and non-static blocks of code for the parent class.
- Executes the constructor of the parent class.
- Implement its own non-static and non-static blocks of code.
- Executes its own constructor.
Here's a simple introduction to static and non-static blocks of code. 1. Static code block:
static {}
2. Non-static code block
{}
The similarities and differences between static code blocks and non-static code blocks are as follows:
- The same point: when the JVM loads the class and executes before the constructor executes, you can define more than one in the class, typically assigning some static variables to the code block.
- Different points: Static code blocks are executed before non-static blocks of code (static code blocks > non-static blocks of code). A static code block executes only once at the first new time and then no longer executes. The non-static code block executes once per new.
The best validation for a conclusion is to write the code to prove the result. First, take a look at the order of execution when class initialization is not inherited, with the following code:
public class Initodertest {public static String Static_field = "static property";//static block static {System.out.println (Static_field); System.out.println ("Static code Block");} Public String field = "Non-static property";//non-static block {System.out.println (field); SYSTEM.OUT.PRINTLN ("Non-static code block");} Public Initodertest () {System.out.println ("no parameter Constructor");} public static void Main (string[] args) {initodertest test = new Initodertest ();}}
Execution Result:
Static property static code block non-static property non-static code block no parameter constructor
Next, let's verify that when the Java class implements inheritance, the order of execution coincides with our conclusion. The test code is as follows:
Class Parenttest {public static String Parent_static_field = "Parent Class-static property";//Parent class-static block static {System.out.println (Parent_ Static_field); System.out.println ("Parent class-Static code block");} public static String ParentField = "Parent class-Non-static property";//Parent class-non-static block {System.out.println (ParentField); System.out.println ("Parent class-Non-static code block");} Public Parenttest () {System.out.println ("parent class-no parameter Constructor");}} public class Initodertest extends Parenttest {public static String Static_field = "static property";//static block static {System.out.println ( Static_field); System.out.println ("Static code Block");} Public String field = "Non-static property";//non-static block {System.out.println (field); SYSTEM.OUT.PRINTLN ("Non-static code block");} Public Initodertest () {System.out.println ("no parameter Constructor");} public static void Main (string[] args) {initodertest test = new Initodertest ();}}
The results of the implementation are as follows:
Parent class-static property parent class-Static code block static property static code block parent class-Non-static property parent class-Non-static code block parent class-parameterless constructor non-static property non-static code block parameterless constructor
Through the program verification, we can see that our conclusion is accurate.
Java class static properties, static blocks, non-static properties, non-static blocks, and the order in which constructors are executed at initialization time