简体   繁体   English

PrintStream对象输出是通过null初始化的,我们如何在其上调用方法?

[英]PrintStream object out is initialized by null, how we call method on it?

I have seen in the System class that the out object (of type PrintStream ) is initialized with a null value. 我已经在System类中看到out对象(类型为PrintStream )用null值初始化。 How can we call method like System.out.prinln(""); 我们如何调用类似System.out.prinln(""); ? In System class out variable initialized like this way: 在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();
     }
}

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. 如上所示,代码out变量由null初始化,并且此变量是最终变量,因此无法进一步初始化,那么我们如何使用“ out”变量。

The explanation is in the comments: 注释中有解释:

/**
 * The following two methods exist because in, out, and err must be
 * initialized to null.  The compiler, however, cannot be permitted to
 * inline access to them, since they are later set to more sensible values
 * by initializeSystemClass().
 */

And initializeSystemClass() uses native methods to initialize the standard streams to non-null values. initializeSystemClass()使用本机方法将标准流初始化为非空值。 Native code can reinitialize variables that are declared final. 本机代码可以重新初始化声明为final的变量。

JVM calls the private static void initializeSystemClass() method which initializes it. JVM调用private static void initializeSystemClass()方法对其进行初始化。

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 . 上面有一篇不错的文章

有一个gettersetterout对象。

When System class get initialized, it calls its initializeSystemClass() method, here is the code: 初始化System类时,它将调用其initializeSystemClass()方法,下面是代码:

FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));

In this code setOut0() is a native function implemented in System.c: 在此代码中,setOut0()是在System.c中实现的本机函数:

JNIEXPORT void JNICALL
Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
{
    jfieldID fid =
        (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
    if (fid == 0)
        return;
    (*env)->SetStaticObjectField(env,cla,fid,stream);
}

This is a standard JNI code that sets System.out to the argument passed to it, this method calls the native method setOut0() which sets the out variable to the appropriate value. 这是一个标准的JNI代码,将System.out设置为传递给它的参数,该方法调用本地方法setOut0() ,该方法将out变量设置为适当的值。

System.out is final, it means it cannot be set to something else in initializeSystemClass() but using native code it is possible to modify a final variable. System.out是最终的,这意味着无法在initializeSystemClass()中将其设置为其他值,但是使用本机代码可以修改最终变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不能在PrintStream类的帮助下调用'println()'方法,其中out是这个类的对象? - Why we can't call 'println()' method with help of PrintStream class where out is object of this class? 我们可以在Java中使用null对象调用静态方法吗? 如果是这样,怎么样? - Can we call a static method with a null object in Java? If so, how? 我们可以在 null object 上调用任何方法吗? - can we call any method on null object? 在Spring和Java中初始化Environment对象后,如何调用方法? - how to call a method after Environment object initialized in Spring and java? 为什么`System.out.println(null);`give“方法println(char [])对于类型PrintStream错误是不明确的”? - Why does `System.out.println(null);` give “The method println(char[]) is ambiguous for the type PrintStream error”? 在方法中初始化后,对象为null - Object is null after being initialized in a method 如何模拟方法中初始化的对象 - How to mock object initialized in method 异常我们有空引用对象但单选按钮已初始化 - Exception we have null reference object but the radio button is initialized 为什么PrintStream.java中的println(Object x)方法从同步块外部调用String.valueOf()? - Why does the println (Object x) method in PrintStream.java call String.valueOf() from outside the synchronized block? 类型PrintStream的方法printf(String,Object ...)不适用于参数(String,void) - The method printf(String, Object…) in the type PrintStream is not applicable for the arguments (String, void)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM