Java static execution order

1. Definition:

  1, Java static variables can only be defined in a class body, it can not be defined in the method. All belong to the class static variable not part of the method.

      2, static blocks: performing the JVM loads class with static stated, performed only once

        Building blocks: the class defined by {}, directly each time the object is created execution
             The priority order of execution: Static blocks> main ()> building block> Constructor
     3, load order the class
       (1) static objects and static parent class code block
       (2) static objects and static subclass code block
       (3) the parent non-static and non-static block objects
       (4) the parent class constructor
       (5) a subclass of non-static and non-static block objects
       (6) subclass constructor
 
   

2, examples

 
public  class the Test {
     static  int AGE1 =. 1 ; 
 
    static { 
        AGE1 = 10 ; 
        System.out.println ( "this is a static block" ); 
    } 
 
    { 
        System.out.println ( "This is a normal block" + AGE1); 
    } 
 
    public the Test () { 
        System.out.println ( "this is the constructor" ); 
    } 
    
    public  void Fun () { 
        System.out.println ( "this is a common method of Fun" ); 
    } 
 
    public  static  void Show () { 
        System.out.println ("This is a static method" + AGE1); 
    } 
 
    public  static  void main (String [] args) { 
        System.err.println ( "==============" ); 
        
        Test.show ( ); 
        
        the Test T = new new the Test (); 
        t.fun (); 
        t.fun (); 
    } 
}


 

3. Source:

   Static Java in order of execution: https://blog.csdn.net/u010859650/article/details/81587738

   java static code execution sequence:  https://www.cnblogs.com/sjxbg/p/8831346.html

  Java static order of execution: https://blog.csdn.net/ewzjs/article/details/78150198

 

Guess you like

Origin www.cnblogs.com/Dream2hc/p/java883134.html