I have the following java code:
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
}
}
Why doesn't the class.forName("Test")
work here while the next line works fine?
Use Outer$Nested
(regardless if nested class is static or not)
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<?> cls = Class.forName("CheckInnerStatic$Test");
//Test test = new Test();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With