Classmeal{ PublicMeal () {System.out.println ("Meal ()--Construction! "); }}classbread{ PublicBread () {System.out.println ("Bread ()--Construction! "); }}classcheese{ PublicCheese () {System.out.println ("Cheese ()--Construction! "); }}classlettuce{ Publiclettuce () {System.out.println ("Lettuce ()--Construction! "); }}classLunchextendsmeal{ PublicLunch () {System.out.println ("Lunch ()--Construction! "); }}classPortablelunchextendslunch{ PublicPortablelunch () {System.out.println ("Portablelunch ()--Construction! "); }} Public classSandwichextendsportablelunch{PrivateBread Bread =NewBread (); PrivateCheese Cheese =NewCheese (); PrivateLettuce lettuce =Newlettuce (); PublicSandwich () {System.out.println ("Sandwich ()--Construction! "); } Public Static voidMain (string[] args) {NewSandwich (); }}
The output results are as follows:
Meal ()--Construction!
Lunch ()--Construction!
Portablelunch ()--Construction!
Bread ()--Construction!
Cheese ()--Construction!
Lettuce ()--Construction!
Sandwich ()--Construction!
Look at another example:
classinsect{Private inti = 9; protected intJ; Publicinsect () {System.out.println ("I=" + i + "j=" +j); J= 39; } Private Static intX1 = printinit ("Static insect X1"); protected Static intPrintinit (String s) {System.out.println (s); return47; }}classBeetleextendsinsect{Private intK = Printinit ("Beetle k"); PublicBeetle () {System.out.println ("k=" + K + "j=" +J );//This j is the parent class J= 25; } Private Static intx2 = Printinit ("Static Beetle X2"); Public Static voidMain (string[] args) {System.out.println ("Beetle Constructor"); Beetle Beetle=NewBeetle ();
System.out.println ();
System.out.println ("Beetle Constructor2");
Beetle beetle2 = new Beetle ();
} }//output result/*static insect x1static Beetle x2beetle constructori=9 j=0beetle k i=47 j=39
Beetle Constructor2
I=9 j=0
Beetle K
k=47 j=39
*/
When running Java on Beetle, the first thing that happens is trying to access Beetle.main () (a static method), so the loader starts and finds out the compiled code for the Beetle class (in the name of the Beetle.class file). As you load it, the compiler notices that it has a parent class (which is known by extends), so it continues to load (the parent class). This happens regardless of whether you intend to produce an object of the parent class (try commenting out the object creation code to prove it).
If the parent class has its own parent class, then the second parent class is loaded, and so on. Next, static initialization in the root parent class (in this case, insect) is executed, followed by the next subclass, and so on. This approach is important because the static initialization of a subclass may depend on whether the parent class member can be initialized correctly.
So far, the necessary classes have been loaded and the objects can be created. First, all the basic types in the object are set to the default values, and the object reference is set to null--, which is generated by setting the object memory to a binary 0 value. The constructor of the parent class is then called. After the parent class constructor is complete, the instance variable is initialized in its order, and the remainder of the last constructor is executed.
Java in think constructor call order