类加载顺序
下面两段代码,我还没弄明白,有时间把解释加上去
第一段
package p1.Demo;
public class Demo17 {
public static void main(String[] args) {
Son son = new Son();
System.out.println("----end----");
}
}
class Son extends Father {
private int a = 1;
private long b = 2L;
static int c = 3;
{
System.out.println("1 Son 动态 block");
}
static {
System.out.println("2 Son static block");
}
Son() {
b = 3L;
System.out.println("3 Son constructor");
}
}
class Father {
int d;
static int e = 4;
static Son son = new Son();
{
System.out.println("4 father 动态 block");
}
static {
System.out.println("5 father static block");
}
Father(){
d = 1;
System.out.println("6 father constructor");
}
}
执行结果
4 father 动态 block
6 father constructor
1 Son 动态 block
3 Son constructor
5 father static block
2 Son static block
4 father 动态 block
6 father constructor
1 Son 动态 block
3 Son constructor
----end----
第二段
package p1.Demo;
public class Demo17 {
public static void main(String[] args) {
Son son = new Son();
System.out.println("----end----");
}
}
class Son extends Father {
{
System.out.println("1 Son 动态 block");
}
static {
System.out.println("2 Son static block");
}
Son() {
System.out.println("3 Son constructor");
}
}
class Father {
{
System.out.println("4 father 动态 block");
}
static {
System.out.println("5 father static block");
}
Father(){
System.out.println("6 father constructor");
}
}
执行结果
5 father static block
2 Son static block
4 father 动态 block
6 father constructor
1 Son 动态 block
3 Son constructor
----end----