Static variables and the order of execution of the code block

class X{

    Y y=new Y();

    public X(){

        System.out.print("X");

    }

}

class Y{

    public Y(){

        System.out.print("Y");

    }

}

public class extends X{

    Y y=new Y();

    public Z(){

        System.out.print("Z");

    }

    public static void main(String[] args) {

        new Z();

    }

}

Initialization process: 

1.  Initialization parent class static member variables and static code block  

2.  Initialization subclass static member variables and static code block  

Common member variables and code blocks 3. Initialize the parent class, then the parent class constructor is executed;

Common member variables and code blocks 4. Initialize the subclass, then the constructor is executed subclass; 

 

Common member variables and code block (1) initialize the parent class, performing new new = Y Y  Y ();   output Y 

The method of construction (2) and then run a parent; output X

(3)  common member variables and initializes subclass code blocks, performing new new = Y Y  Y ();  output Y    

Constructor (4) again performs the subclass; Output Z

 The output YXYZ

 

to sum up

  In Java static variables and the time code block is loaded on the implementation of the class, instance objects, instance variables and declare the constructor is executed again. Subclasses inherit parent class, the static variables and to execute the parent class code block, then perform a static variable and static subclass code block. Similarly, followed by the implementation of the parent class and subclass of the non-static block of code, and constructors.

Note :( static) and variables (static) is also a code block execution order consistent with the order of the code written. In the (static) can use the code block (static) variable, but was used (static) variable must be in the (static) block previously declared.

 Finally, perform these steps:

1, the parent class static variables and code block (declare the first);

2, subclass static variables and code block (declare the first);

3, the parent class variables and code blocks (first declared first);

4, the parent class constructor;

5, class variables and sub code blocks (first declared first);

6, the configuration of the subclass.

 

1. The parent class static block of code (java virtual machine when loading a class, the block of code will be executed, so execution only once)

2. Subclass static block of code (java virtual machine when loading a class, the block of code will be executed, so execution only once)

3.  parent class object initialization properties

4. Common parent block (each new, executed each time  )

The  parent class constructor (each new, executed each time)

6. The sub-  class  attribute object initialization

 

7. subclasses normal code blocks (each new, executed each time  )

8. The sub-  class constructor (each new, executed each time)

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/91628328