问题描述
JVM调用private static void
initializeSystemClass()
初始化它的方法。
参见这两行代码:
setout0(new PrintStream(new bufferedoutputstream(fdOut, 128), true));
setErr0(new PrintStream(new bufferedoutputstream(fdErr, 128), true));
这是两种本机方法:
private static native void setout0(PrintStream out);
private static native void setErr0(PrintStream err);
有一篇不错的文章。
解决方法
我在System
该类中看到,该out
对象(类型为PrintStream
)已用一个null
值初始化。我们该如何调用like方法
System.out.prinln("");
?在System类中,以这种方式初始化out变量:
package java.lang;
public final class System {
public final static PrintStream out = nullPrintStream();
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
}
如上所示,代码out
变量由null初始化,并且此变量是最终变量,因此无法进一步初始化,那么我们如何使用“ out”变量。