先按从基类到导出类的顺序初始化静态域(之前已经初始化过的静态域不再初始化)

再按从基类到导出类的顺序初始化类,即基类普通字段+基类构造器主体+导出类字段+导出类主体...

package test;

class PrintInfo{
static String Info(String s){
System.out.println(s);
return s;
}
}
class grandfather{
static String grandfatherstatic=PrintInfo.Info("grandfatherstatic1");
static String grandfastatic2=PrintInfo.Info("grandfatherstatic2");
String fa=PrintInfo.Info("grandfatherString1");
String fa2=PrintInfo.Info("grandfatherString2");
grandfather(){
PrintInfo.Info("grandfatherconstructor");
}
}
class father extends grandfather{
static String fatherstatic=PrintInfo.Info("fatherstatic1");
static String fastatic2=PrintInfo.Info("fatherstatic2");
String fa=PrintInfo.Info("fatherString1");
String fa2=PrintInfo.Info("fatherString2");
father(){
PrintInfo.Info("fatherconstructor");
} }
class son extends father{
static String sonstatic=PrintInfo.Info("sonstatic1");
static String sonstatic2=PrintInfo.Info("sonstatic2");
String son=PrintInfo.Info("sonString1");
String son2=PrintInfo.Info("sonString2");
son(){
PrintInfo.Info("sonconstructor");
}
}
public class Test {
public static void main(String[] args) {
new son();
}
} output:

grandfatherstatic1
grandfatherstatic2
fatherstatic1
fatherstatic2
sonstatic1
sonstatic2
grandfatherString1
grandfatherString2
grandfatherconstructor
fatherString1
fatherString2
fatherconstructor
sonString1
sonString2
sonconstructor

05-08 08:08