Execution order in a class:
static variables
Static code block
code block
Construction method
Subclass and Parent class execution order:
Parent class static variable
Parent class static code block
Sub-class static variables
Sub-class static code block
Parent Class code block
Parent class Construction Method
Subclass code Block
Sub-class construction methods
See the code directly, at a glance.
Public classTest { Public Static voidMain (string[] args) {NewA (); /*Result: * A static variable A static code block a code block a construction method **/ NewB (); /** Result: * A static variable a static code block b static variable b static code block a code block a construction method B code block B construction Method **/ }}classa{Staticc C =NewC ("A"); {System.out.println ("A code block"); } Static{System.out.println ("A static code block"); } PublicA () {System.out.println ("A construction method"); }}classBextendsa{Staticc C =NewC ("B"); {System.out.println ("B code block"); } Static{System.out.println ("B Static code block"); } PublicB () {System.out.println ("B Construction Method"); } }classC { PublicC (String str) {System.out.println (str+ "Static variable"); }}
Java code blocks, static code blocks, static variables, construction method execution order