java成员加载顺序(引)
此篇仅仅是提出当包含继承及静态变量(代码块)时的执行顺序
一、代码
/**
* @author :spj
* @Date:2022/5/12
* @description:父
*/
@SuppressWarnings("all")
public class Father {
static {
System.out.println(1);
}
{
System.out.println(2);
}
public Father(){
System.out.println(3);
}
}
/**
* @author :spj
* @Date:2022/5/12
* @description:子
*/
@SuppressWarnings("all")
public class Son extends Father {
public static void main(String[] args) {
Son son = new Son();
}
static Son SON = new Son();
static {
System.out.println(4);
}
{
System.out.println(5);
}
public Son() {
System.out.println(6);
}
}
二、效果
1
2
3
5
6
4
2
3
5
6
由此可见代码并非完全按照顺序执行