Subclass instantiation involves two steps:
<1>. class-relatedStatic contentInitialization;
* Parent class and Child class:
1. Static attributes of the parent class;
2. Static block of the parent class;
3. Static attributes of subclass;
4. Static blocks of child classes;
<2>.Instance contentInitialization;
* Call construction sub-: first parent class and then subclass: instance fields and blocks are instantiated before a sub-call is constructed
5. common attributes of the parent class;
6. Common blocks of the parent class;
7. constructor of the parent class;
8. common attributes of sub-classes;
9. Common blocks of sub-classes;
10. constructor of subclass;
Example:
Package basic; public class clzinit {public static void main (string [] ARGs) {New son () ;}} Class Parent {protected int n = 5; protected static int M = 5; static {M = m * 2; system. out. println ("parent class static block call; M =" + M) ;}{ n = N * 2; M = m * 2; system. out. print ("parent class common block call;"); system. out. print ("n =" + n); system. out. println ("m =" + M);} public parent () {This. N = N * 10; M = m + 10; system. out. print ("parent class constructor; n =" + n); system. out. println ("m =" + M) ;}} class son extends parent {private int Sn = 3; Private Static int Sm = 3; static {M = m + 2; sm = Sm + 2; system. out. println ("subclass static block call; M =" + M) ;}{ n = n + 2; Sn = Sn + 2; M = m + 2; system. out. println ("subclass common block call;"); system. out. print ("n =" + n); system. out. print ("Sn =" + SN); system. out. println ("m =" + M);} public son () {This. N = N + 10; Sn = Sn + 10; M = m + 10; system. out. print ("subclass constructor; n =" + n); system. out. println ("m =" + M );}}
Looking at the output results, we can see the clues:
Parent class static block call; M = 10 subclass static block call; M = 12 parent class common block call; n = 10 m = 24 parent class constructor; N = 100 m = 34 subclass common block call; n = 102sn = 5 m = 36 subclass constructor; n = 112 m = 46
Memo. Thank you for your correction!