/**
* 测试静态代码块执行顺序
* 1、static能修饰的类只有静态内部类
* 2、静态方法不能直接访问非静态成员(方法,成员变量)
* 3、静态代码块在类加载的时候,就直接加载,且只执行一次
* 4、子类会默认调用父类的无参构造
* 5,执行顺序:父类静态代码块与静态成员-->
* 子类静态代码块与静态成员-->
* 父类代码块-->
* 父类构造方法-->
* 子类代码块-->
* 子类构造方法-->
*/
public abstract class AA {
public static void main(String[] args) {
AA aa=new BB("aa");
aa=new BB("aa");
}
static {
System.out.println(1);
}
{
System.out.println(11);
}
public AA() {
System.out.println("a");
}
public AA(String a) {
System.out.println("a:"+a);
}
}
class BB extends AA{
static {
System.out.println(2);
}
{
System.out.println(22);
}
public BB() {
System.out.println("b");
}
public BB(String b) {
System.out.println("b:"+b);
}
}
输出结果
1
2
11
a
22
b:aa
11
a
22
b:aa