Dimz
2020-09-04T12:33:22+00:00
public class Test {
static {
System.out.println(new Test2().getA()+" f static block");
}
public static int a=55;
public Test() {
System.out.println("f constructor");
}
public int getA() {
return a;
}
}
class Test2 extends Test {
static {
System.out.println(new Test2().getA()+" s static block");
}
public static int a=666;
public Test2() {
System.out.println("s constructor");
}
@Override
public int getA() {
return a+100;
}
public static void main(String[] args) {
System.out.println("main");
}
}
输出结果static {
System.out.println(new Test2().getA()+" f static block");
}
public static int a=55;
public Test() {
System.out.println("f constructor");
}
public int getA() {
return a;
}
}
class Test2 extends Test {
static {
System.out.println(new Test2().getA()+" s static block");
}
public static int a=666;
public Test2() {
System.out.println("s constructor");
}
@Override
public int getA() {
return a+100;
}
public static void main(String[] args) {
System.out.println("main");
}
}
f constructor
s constructor
100 f static block
f constructor
s constructor
100 s static block
main
s constructor
100 f static block
f constructor
s constructor
100 s static block
main
初学jvm,发现对象可以在准备阶段前就创建,虚方法表可以在准备阶段前完成,我现在是没法理解什么原理???