锘??xml version="1.0" encoding="utf-8" standalone="yes"?> //: c06:Beetle.java //: c6/P23.java Ressult of run P23:
// initilized the base-class and the derived-class static fileds: 聽聽聽 a. initized the base-class and the derived-class static fields 聽聽聽 b. and then when new the derived class instance, e,g new P23(), need set default value to the non-static fields and call the just class's constructors for base class, and trun to the derived class, i.e. P23 at last. 聽聽聽聽 work like this one by one
2.欏圭洰綆$悊鐨勫厤璐癸紙鍜屽嚑涔庢槸鍏嶈垂鐨勶級宸ュ叿
http://se.csai.cn/casepanel/Other/No001.htm
// The full process of initilization.
// first load base-class, and static filed will be initilized:
class Insect {
聽int i = 9;
聽int j;
聽Insect() {
聽聽prt("i = " + i + ", j = " + j);
聽聽j = 39;
聽}
聽static int x1 = prt("static Insect.x1 initilized");
聽static int prt(String s) {
聽聽System.out.println(s);
聽聽return 47;
聽}
}
// then load derived class, & the static fileds will be initilized:
// after this, u can create object now, first of all
// all the base fileds will be set to default value(reference
// will be set to "null"
// then call the base-class constructor --> call derived class
// constructor.
public class Beetle extends Insect {
聽int k = prt("Beetle.k initilized");
聽Beetle() {
聽聽prt("k = " + k);
聽聽prt("j = " + j);
聽聽j = 56;
聽}
聽static int x2 = prt("static Beetle.x2 initilized");
聽public static void main(String[] args) {
聽聽prt("Beetle constructor");
聽聽Beetle b = new Beetle();
聽}
}///:~
// class loaded and initilization
public class P23 extends Beetle {
聽int m = prt("P23.m initilized");
聽P23() {
聽聽prt("m = " + m);
聽聽prt("j = " + j);
聽}
聽// will initilized after the static fileds of Beetle:
聽static int x3 = prt("static P23.x3 initilized");
聽public static void main(String[] args) {
聽聽// begin to run base-class constructor
聽聽prt("P23 constructor");
聽聽P23 p = new P23();
聽}
}
static Insect.x1 initilized
static Beetle.x2 initilized
static P23.x3 initilized
// set default value to the non-static fileds
// call the very class's聽constructors
// work like this one by one, and class P23 at last:
P23 constructor
i = 9, j = 0
Beetle.k initilized
k = 47
j = 39
P23.m initilized
m = 47
j = 56
=========================
鎬葷粨錛?br />1) when run the derived-class, jvm first load all relevant class object, at this time will do:
--浠ヤ笂<<鎽樿嚜Java閭歡寮鍙戣瑙?gt;>