w*********n 发帖数: 439 | 1 class Super {
static String ID = "QBANK";
}
class Sub extends Super{
static {System.out.println("In sub"); }
}
public class TestClass {
public static void main(String[] args) {
System.out.println(Sub.ID);
}
}
请问Sub里面的静态block为什么不运行? |
w**z 发帖数: 8232 | 2 ID is static , no Sub instance created.
【在 w*********n 的大作中提到】 : class Super { : static String ID = "QBANK"; : } : class Sub extends Super{ : static {System.out.println("In sub"); } : } : public class TestClass { : public static void main(String[] args) { : System.out.println(Sub.ID); : }
|
w*********n 发帖数: 439 | 3 多谢回答,
JAVA书上说static block 在class加载的时候运行一次,
请问Sub.ID这句不是加载了Sub class吗? |
w**z 发帖数: 8232 | 4 it only references the static variable of the superclass, the subclass is
not initialized and seems like JVM thinks it is not necessary to even load
the subclass.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jl
【在 w*********n 的大作中提到】 : 多谢回答, : JAVA书上说static block 在class加载的时候运行一次, : 请问Sub.ID这句不是加载了Sub class吗?
|