先后顺序如下!
1.静态块
2.块
3.构造器
4.父类构造器
运行顺序为:
1. 静态块
2. 父类构造器
3. 本类中的块
4. 本类的构造器
父类:
package test922;
public class Father {
static {
System.out.println("Father static方法");
}
{
System.out.println("Father 块方法");
}
public Father() {
System.out.println("Father构造器");
}
public void usualFatherFunction() {
System.out.println("usualFatherFunction父类普通方法");
}
}
本类:
/**
*
*/
package test922;
/**
* @author rocling
*/
public class Oneself extends Father {
static {
System.out.println("Oneself static方法");
}
{
System.out.println("oneself 块方法");
}
/**
* 本地构造器
*/
public Oneself() {
System.out.println("oneself构造器");
}
public void usualOneselfFunction() {
System.out.println();
}
/**
* @param args
*/
public static void main(String[] args) {
Oneself oneself = new Oneself();
oneself.usualOneselfFunction();
oneself.usualFatherFunction(); // static只执行一次
}
}
结果:
Father static方法
Oneself static方法
Father 块方法
Father构造器
oneself 块方法
oneself构造器usualFatherFunction父类普通方法