我是java新手,为了清楚“System.out”,我阅读了相关的java源代码,然后找到了我无法理解的东西。首先是“System.out”的源代码:
public final static PrintStream out = nullPrintStream();
然后我去了nullPrintStream
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
NullPointerException
我的问题是:程序可能会在函数中抛出 a nullPrintStream()
,我们不需要在public final static PrintStream out = nullPrintStream();
? 为了清楚起见,我在 Eclipse 中编写了一些测试代码,如下所示:
package MainPackage;
public class Src {
private static int throwException() throws Exception{
int m = 1;
if(m == 0) {
throw new Exception();
}
return 0;
}
public static final int aTestObject = throwException(); <==Here i got an error
public static void main(String args[]) {
}
}
就像我想的那样,我收到了一个错误Unhandled exception type Exception,但是为什么System.out
不使用 Exception 就可以了NullPointerException
?