以下是 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将通过setOutSystem 类中的以下方法设置
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
他们为什么 JLS 需要nullPrintStream方法?