An example illustrates the order in which code blocks are executed in Java

  • 2020-04-01 04:05:09
  • OfStack

preface
      Today, when I was looking at the Android ContentProvider implementation, I suddenly thought about the execution order of static fields, static blocks, non-static fields, non-static blocks, and constructors in the Java class new. In fact, this is a very classic question, very much to the extent of the basic knowledge of Java. Many interviews will ask you this question, so take time over the weekend to review it.

conclusion
      So I'm going to throw out the results, and then I'm going to write a program to verify our results. In the process of Java class being new, the execution order is as follows:

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

      In the process of realizing the inherited class to be new, the execution order of initialization is as follows:

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

      A brief introduction to static and non-static code blocks is required.
1. Static code block:

      The static { 
      }  

2. Non-static code blocks

      { 
      }  

      The similarities and differences between static and non-static code blocks are as follows:

      Similarities: both classes are loaded by the JVM and executed before the constructor is executed, and multiple static variables can be defined in the class. In general, some static variables are assigned in the code block.       Differences: static code blocks are executed before non-static code blocks (static code blocks > Non-static code block. A static block of code executes only once on the first new and then no more. Instead of a static block of code executing every new time.


validation
      The best way to verify a conclusion is to write code to prove the result. First, let's take a look at the execution order of the class initialization without inheritance. The code is as follows:

 


  public class InitOderTest { 
    public static String STATIC_FIELD = " Static attributes "; 
     
    //A static block
    static { 
      System.out.println(STATIC_FIELD); 
      System.out.println(" Static code block "); 
    } 
     
    public String field = " Nonstatic attribute "; 
     
    //The static block
    { 
      System.out.println(field); 
      System.out.println(" Non-static code blocks "); 
    } 
   
    public InitOderTest() { 
      System.out.println(" Parameter free constructor "); 
    } 
     
    public static void main(String[] args) { 
      InitOderTest test = new InitOderTest(); 
    } 
  } 

      Execution results:

      Static property         Static code block         Non-static property         Non-static code block         Parameter free constructor  

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

     


 class ParentTest { 
    public static String PARENT_STATIC_FIELD = " The parent class - Static attributes "; 
   
    //Parent - static block
    static { 
      System.out.println(PARENT_STATIC_FIELD); 
      System.out.println(" The parent class - Static code block "); 
    } 
   
    public static String parentField = " The parent class - Nonstatic attribute "; 
   
    //Parent - non-static block
    { 
      System.out.println(parentField); 
      System.out.println(" The parent class - Non-static code blocks "); 
    } 
   
    public ParentTest() { 
      System.out.println(" Superclass - parameter free constructor "); 
    } 
  } 
   
  public class InitOderTest extends ParentTest { 
    public static String STATIC_FIELD = " Static attributes "; 
   
    //A static block
    static { 
      System.out.println(STATIC_FIELD); 
      System.out.println(" Static code block "); 
    } 
   
    public String field = " Nonstatic attribute "; 
   
    //The static block
    { 
      System.out.println(field); 
      System.out.println(" Non-static code blocks "); 
    } 
   
    public InitOderTest() { 
      System.out.println(" Parameter free constructor "); 
    } 
   
    public static void main(String[] args) { 
      InitOderTest test = new InitOderTest(); 
    } 
  } 

      The implementation results are as follows:

      Superclass - static property         Superclass - static code block         Static property         Static code block         Superclass - non-static property         Superclass - non-static code block         Superclass - parameter-free constructor         Non-static property         Non-static code block         Parameter free constructor

Related articles: