Illustrates the execution order of code blocks in Java _java

Source: Internet
Author: User

Objective
today, when I look at the Android ContentProvider implementation, 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 explore the knowledge of Java basics. Many interviews are believed to have such problems, taking advantage of the weekend to have time to review.

Conclusion
Here we put the finishing conclusions aside and I'm writing a program to validate our conclusions. In the process of the Java class being new, the order of execution is as follows:

    • Implement its own static properties and static code blocks. (Decide who executes first, depending on the order in which the code appears)
    • Implement its own non-static properties and non-static blocks of code.
    • Executes its own constructor.

In the process of implementing the inherited class being new, the execution order is initialized as follows:

    • Implements public static properties and static block-level code for the parent class.
    • Implement its own static properties and static block-level code.
    • Implements a Non-static property and a non-static code block for the parent class.
    • Executes the constructor of the parent class.
    • Implement its own non-static properties and non-static blocks of code.
    • Executes its own constructor.

Here is a simple introduction to static code blocks and non-static blocks of code.
1. Static code block:

static {
}

2. Non-static code block

{
}

The similarities and differences between the static code block and the Non-static code block are as follows:

    • The same point: both the JVM loads the class and executes before the constructor executes, and multiple can be defined in the class, typically assigning some static variables to the code block.
    • Different points: Static code blocks are executed before a non-static code block (static code block > Non-static code block). The static code block is executed only once on the first new time, and then no longer executes. Non-static code blocks are executed once every new time.


Verify
The best validation of the conclusion is to write the code to prove the result. First, take a look at the order in which the classes are executed without inheriting, as follows:

  public class Initodertest {public 
    static String Static_field = "static property"; 
     
    Stationary 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 ("parameterless constructor"); 
    } 
     
    public static void Main (string[] args) { 
      initodertest test = new Initodertest (); 
    } 
   

Execution results:

    • Static properties
    • Static code block
    • Non-static properties
    • Non-static code block
    • Non-parametric constructor

Next, we verify that the execution order matches our conclusion when the Java class implements inheritance. The test code is as follows:

 Class Parenttest {public static String Parent_static_field = "Parent Class-static property"; 
      Parent classes-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-parameterless constructor"); 
   
    The public class Initodertest extends Parenttest {public static String Static_field = "static Property"; 
      Stationary 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 ("parameterless constructor"); 
    public static void Main (string[] args) {initodertest test = new Initodertest (); }
  } 
 

The results of the implementation 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-parameterless constructor
    • Non-static properties
    • Non-static code block
    • Non-parametric constructor
Related Article

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.