1. Output order of static code block, non-static code block, constructor function
Case one: When the parent class is not inherited
1 classHelloa {2 3 PublicHelloa () {4System.out.println ("I ' M A class");5 }6 7 Static {8System.out.println ("Static A");9 }Ten One { ASystem.out.println ("A"); - } - the Public Static voidMain (string[] args) { - NewHelloa (); - NewHelloa (); - } +}
Output Result:
Static A
A
I ' M A class
A
I ' M A class
Analysis: The output order is: Static code block > Non-static code block > constructor, and no matter how many times the class static code block is executed only once, so the general situation of memory-intensive connection or other in the static code block, access speed, and only access once, save consumption.
Case two: When inheriting from a parent class
classHelloaextendshellob{ PublicHelloa () {System.out.println ("I ' M A class"); } Static{System.out.println ("Static A"); } {System.out.println (A); } Public Static voidMain (string[] args) {NewHelloa (); }}classHellob { PublicHellob () {System.out.println ("I ' M B class"); } Static{System.out.println ("Static B"); } {System.out.println (B); }}
Operation Result:
Static B
Static A
B
I ' M B class
A
I ' M A class
Parsing: Output order: Parent class static code block > Subclass Static code block > parent non-static code block > Parent class constructor > subclass non-static code block > Subclass constructor;
Java Common pen Questions < two >