我有这段代码,我正在使用 -verbose:class 选项运行它以查看加载的类。令我惊讶的是,它显示它加载了 A1 和 A2 类,但未调用静态块。
有人可以解释这种行为吗
package P1;
import java.lang.reflect.InvocationTargetException;
public class DemoReflection {
static {
System.out.println("Loading Demo");
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("A2 " + A2.class.getClassLoader().getClass());
System.out.println("Demo " + DemoReflection.class.getClassLoader().getClass());
System.out.println("A1 " + A1.class.getClassLoader().getClass());
}
}
class A1 {
static {
System.out.println("Loading A1");
}
}
class A2 extends A1 {
static {
System.out.println("Loading A2");
}
public A2() {
System.out.println("m2");
}
public void m1() {
System.out.println("m1");
}
}
class A3 {
static int a3Id = 3;
static {
System.out.println("Loading A3");
}
}
输出:
湖上湖
阿波罗的战车
相关分类