PrintStream对象out由null初始化,我们如何调用它的方法? [英] PrintStream object out is initialized by null, how we call method on it?

查看:467
本文介绍了PrintStream对象out由null初始化,我们如何调用它的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 System 类中看到 out 对象(类型为 PrintStream) )使用 null 值初始化。我们如何调用 System.out.prinln(); 等方法?
在这样的System class out变量中初始化:

  package java.lang; 

public final class System {
public final static PrintStream out = nullPrintStream();

private static PrintStream nullPrintStream()抛出NullPointerException {
if(currentTimeMillis()> 0){
return null;
}
抛出新的NullPointerException();
}
}

如上所示代码 out 由null初始化的变量,这个变量是final,所以它不能进一步初始化,那么我们如何使用out变量。

解决方案

JVM调用 私有静态无效initializeSystemClass() 方法,该方法对其进行初始化。



'p>请参阅的这两行代码:

  setOut0(新的PrintStream(新的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);

有一个上面的好文章


I have seen in the System class that the out object (of type PrintStream) is initialized with a null value. How can we call method like System.out.prinln("");? In System class out variable initialized like this way:

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();
     }
}

As per shown above code out variable initialized by null and this variable is final, so it can not initialized further then how can we use "out" variable.

解决方案

JVM calls the private static void initializeSystemClass() method which initializes it.

See these two lines of code :

setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));

These are the two native methods :

private static native void setOut0(PrintStream out);
private static native void setErr0(PrintStream err);

There is a nice article on it.

这篇关于PrintStream对象out由null初始化,我们如何调用它的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆