以下是java.lang.System类中的代码(JDK版本1.6)
public final static PrintStream out = nullPrintStream(); //out is set to 'null'
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
当我们写System.out.println(“Something”);在我们的代码中,为什么即使’out’设置为’null’也不会得到NullPointerException
无论如何out将通过System类中的以下setOut方法设置
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
Theyn为什么JLS需要nullPrintStream方法?