代码
package test;
class MainFather{
static {System.out.println("this is in MainFather");}
MainFather(){System.out.println("this is in MAINFATEHR,CONSTRUCTOR");}
}
public class Main extends MainFather{
public static void main(String[] Args){
new Main().new test().hello();
new StaticClass();
hi();
System.out.println("world");
}
static {
System.out.println("hello");
}
static void hi(){
System.out.println("hi,I am in main,static member method");
}
class test{
void hello(){
System.out.println("this inner test");
}
}
}
class StaticClass {
static {
System.out.println("hi");
}
StaticClass(){
System.out.println("this is in Constructor");
}
}
结果
结果分析
父类静态代码块→子类静态代码块→父类构造方法→子类构造方法→正常函数执行顺序