The order in which free blocks are executed in Java

  • 2020-04-01 01:41:02
  • OfStack

Free blocks in Java are divided into static and non-static free blocks.The execution time of a non-static free block is: before the constructor is executed. The execution time of the static free block is: when the class file is loaded. Non-static free blocks can be executed multiple times, as long as an object is initialized, but static free blocks are executed only once when the class is loaded, typically to initialize the value of a class's static variable. Each time an object is initialized, a non-static block is executed. If inheritance is involved, it is: first execute the non-static block of the parent class, then the constructor of the parent class, then its own free block, and finally its own constructor. The execution time of the static block is when the class file is loaded. The class file is loaded only once, so the static block is executed only once. When the class is used again, the static block is not executed again.   Static blocks are executed during the initialization phase after the class is loaded. If you use ClassLoader's loadclass to simply load the class without initializing it, you will not trigger the execution of a static block. When the default initialize value is true, the Class forname (String) is used. If you use forname (String name, Boolean initialize, ClassLoader loader) and set initialize to false, a static block will not be executed. The initialization phase after class loading includes: run < Clinit> Method, which is the class variable initialization statement and the static free block statement. This method is a call that cannot be displayed after the information is collected by the Java compiler.

Here is an example:

The parent class


father.java
public class father {
    static{//A static block
       System.out.println("father'sSTATIC free block running");
    }
    {// non A static block
 System.out.println("father'sfree block running");
    }
    public father(){
       System.out.println("father'sconstructor running");
    }
}

A subclass


son.java
public class son extends father{
    static{//A static block
       System.out.println("son'sSTATIC free block running");
    }
    {// non A static block
       System.out.println("son's freeblock running");
    }
    public son() {
       // TODO Auto-generated constructor stub
       System.out.println("son'sconstructor running");
    }
}

The class of the main function


test.java
public class test{
      public static void main(String[] args) {
       Class f;
       try {
           System.out.println("--------beforeload father--------");
           f=Class.forName("freeblock.father");
           System.out.println("--------afterload father---------");         
           System.out.println("--------beforeinitial father object--------");
           f.newInstance();
           System.out.println("--------afterinitial father object--------");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch (InstantiationException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       }      
       Class s;
       try {
           System.out.println("-------beforeload son--------");
           s=Class.forName("freeblock.son");
           System.out.println("--------afterload son-------");
           System.out.println("--------beforeinitial son object----------");
           s.newInstance();
           System.out.println("--------afterinitial son object-----------");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch (InstantiationException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       }
    }
}

Execution results:

-- -- -- -- -- -- -- -- before loadfather -- -- -- -- -- -- -- --

Father 's STATIC free blockrunning

-- -- -- -- -- -- -- -- after loadfather -- -- -- -- -- -- -- -- --

-- -- -- -- -- -- -- -- before initial fatherobject -- -- -- -- -- -- -- --

Father 's free block running

Father 's constructor running

-- -- -- -- -- -- -- -- after initial fatherobject -- -- -- -- -- -- -- --

-- -- -- -- -- -- -- before the load son -- -- -- -- -- -- -- --

Son's STATIC free block running

-- -- -- -- -- -- -- -- after the load son -- -- -- -- -- -- --

-- -- -- -- -- -- -- -- before initial sonobject -- -- -- -- -- -- -- -- -- --

Father 's free block running

Father 's constructor running

Son 's free block running

Son 's constructor running

-- -- -- -- -- -- -- -- after initial son object -- -- -- -- -- -- -- -- -- -- -

 


Related articles: