To do a simple transcript, it is to revisit the basics.
1. Look at the code first:
Package Com.test;public class Test {public static void Main (string[] args) { Son s = new Son ()} } class Par ent{int parent = ten; { System.out.println ("initialization block in parent"); } static{ System.out.println ("Static initialization block in parent"); } Public parent () { System.out.println ("Parent constructor Method");} class Son extends parent{int son = ten; { System.out.println ("initialization block in Son"); } static{ System.out.println ("Static initialization block in Son"); } Public son () { System.out.println ("son constructs Method"); } }
Execution Result:
2. Variables and static variables
From the above we can see the static statement block and the execution order of the constructors, then how to instantiate the member variables and static variables?
Visible instance members and instance initialization blocks, executed sequentially in the order in which they appear in the code
3. Summary
Properties, methods, construction methods, and free blocks are members of the class, and the order in which members of the class are executed when the object of the class is created:
1). The parent class static members and static initialization are fast, executed sequentially in the order in which they appear in the code.
2). Subclass static members and static initialization blocks, executed sequentially in the order in which they appear in the code.
3). Instance members and instance initialization blocks of the parent class are executed sequentially in the order in which they appear in the code.
4). Executes the construction method of the parent class.
5). Subclass instance members and instance initialization blocks, executed sequentially in the order in which they appear in the code.
6). The constructor method of the subclass is executed.
Member initialization order for Java classes