Java class static properties, static blocks, non-static properties, non-static blocks, and the order in which constructors are executed at initialization time

Source: Internet
Author: User

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:
    1. Implement its own static properties and static blocks of code. (determines who executes first, based on the order in which the code appears)
    2. Implement its own non-static and non-static blocks of code.
    3. Executes its own constructor.
In the process of implementing the inherited class being new, the initialization execution order is as follows:
    1. Implements the public static properties and static block-level code for the parent class.
    2. Implement its own static properties and static block-level code.
    3. Implements non-static and non-static blocks of code for the parent class.
    4. Executes the constructor of the parent class.
    5. Implement its own non-static and non-static blocks of code.
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.