如何在Java中加载嵌套类?
2022-09-02 11:35:04
我有以下java代码:
public class CheckInnerStatic {
private static class Test {
static {
System.out.println("Static block initialized");
}
public Test () {
System.out.println("Constructor called");
}
}
public static void main (String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
System.out.println("Inside main");
Class.forName("Test"); // Doesn't work, gives ClassNotFoundException
//Test test = new Test(); // Works fine
}
}
为什么这里的工作不工作,而下一行工作正常?class.forName("Test")