strange 'out' variable, System.out.println()

后端 未结 3 1859
再見小時候
再見小時候 2020-12-11 06:18

Following is the code from java.lang.System class (JDK version 1.6)

public final static PrintStream out = nullPrintStream(); //out is set to \'null\'

priva         


        
3条回答
  •  清歌不尽
    2020-12-11 06:49

    Take a look at the private static void initializeSystemClass() - this method is called to start things up, it calls setOut0() which is a native method. This ties the Stream into where it's supposed to be.

    So even though the field may look public static final it actually isn't, the native code changes it.

    EDIT

    OP asks Then why JLS needs nullPrintStream method?

    This is to do with the java compiler - it will "inline" static final fields if they are assigned to something constant at compile time, like null. The compiler will actually replace each reference to the field with the constant.

    This would break the initialisation as objects would no longer hold a reference to the Stream but to null. Assigning the stream to the return of a method prevents the inlining.

    Some might call it a dirty hack. To misquote Bismarck "The JDK is like sausages, it's best not to see it being made".

提交回复
热议问题