2
运行此程序后我得到java.lang.InstantiationException
,但我期待作为输出的Hello World。InstantiationException对内部类
public class Test {
public static void main(String[] args) throws Exception {
new Test().greetWorld();
}
private void greetWorld() throws Exception {
System.out.println(Test2.class.newInstance());
}
class Test2 {
public String toString() {
return "Hello world";
}
}
}
有没有人告诉我关于这个例外,它为什么不打印的Hello World
在Java中没有这样的“静态内部类”! –
@LewBloch你是什么意思,没有静态的内部类? – Sweeper
我的意思是说,Java中“内部类”的定义是不是“静态”的嵌套类,所以根据定义,Java中不存在“静态内部类”。 –