Packagecom.cwcec.test;classfu{intnum = 5; //Building Code Blocks{System.out.println ("Fu Constructor code ..." +num); Num=6; } PublicFu () {System.out.println ("Fu constructor ..." +num); Show (); } voidShow () {System.out.println ("Show ..." +num); } }classZiextendsfu{intnum = 9; //Building Code Blocks{System.out.println ("Zi Constructor code ..." +num); Num= 10; } PublicZi () {System.out.println ("Zi constructor ..." +num); } voidShow () {System.out.println ("Show ..." +num); }} Public classFielddemo { Public Static voidMain (string[] args) {NewZi (); } }
Output:Fu Constructor Code ... 5Fu Constructor ... 6show ... 0Zi Constructor Code ... 9Zi Constructor ... Ten
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////
classanimal{ PublicString name = "Ti"; Public Static intAge = 10; Static{System.out.println ("Animal Age:" +Age ); } PublicAnimal () { age= 100; System.out.println ("Animal constructor ..."); } Public voiddisplay () {System.out.println ("Name:" +name); } }classDogextendsanimal{ Public Static intSize = 20; Static{System.out.println ("Dog Size:" +size); } PublicDog () {size= 200; System.out.println ("Dog constructor ..."); } Public Static voidsleep () {System.out.println ("Dog sleep ..."); } } Public classsingle{ Public Static voidMain (string[] args) {Dog dog=NewDog (); }}
Summarize
1. The approximate order of implementation is as follows,
(1) In a class that does not exist: Initializes a static variable, performs a static initialization, and initializes a normal member variable (if there is an assignment statement), performs a normal initialization block--construction method
(2) In an inherited class: Initializes the parent class static member variable, runs the parent class static initialization block, initializes the subclass static member variable, runs the subclass static initialization block, and initializes the parent class instance member variable (if there is an assignment statement). Executes the parent class initialization block--the parent class constructor Method--initializes the subclass instance member variable (if there is an assignment statement) and the normal initialization block---subclass construction method.
Java member Initialization Order