1 Public classSTR {2 3 Static {4System.out.println ("Static{1}");5 }6 Static {7System.out.println ("Static{2}");8 }9 {TenSystem.out.println ("{}"); One } A - Str () { -System.out.println ("Str ()"); the } - - Public Static voidMain (string[] args) { - NewStr (); + NewStr (); - } +}
Output Result:
Static{1}
STATIC{2}
{}
STR ()
{}
STR ()
Conclusion:
1. Static code blocks are executed sequentially and executed once (class loading is performed)
2. Constructs a code block, executes each time the new object is executed, and executes before the constructor
3. Execute the constructor
Understand:
Static code blocks are static, owned by the class, executed when the class is loaded, and construction blocks are non-static, owned by the object, and executed when the object is constructed.
Static code blocks, construction code blocks, execution of constructors