Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}

when we write System.out.println("Something"); in our code then why don't we get NullPointerException even when 'out' is set to 'null'

Anyhow out will be set via following setOut method in System class

public static void setOut(PrintStream out) {
     checkIO();
     setOut0(out);
 }

Theyn why JLS needs nullPrintStream method ?

like image 285
AmitG Avatar asked Mar 22 '13 08:03

AmitG


3 Answers

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".

like image 186
Boris the Spider Avatar answered Nov 16 '22 18:11

Boris the Spider


System.in, out, and err are managed by the JVM from native code. This whole magic with nullPrintStream() was there to keep javac from inlining these fields. Since java 7 it looks like

public final static PrintStream out = null;
like image 40
Denis Tulskiy Avatar answered Nov 16 '22 19:11

Denis Tulskiy


This is just how the System.out class is intialised.

There is also a method:

 private static native void setOut0(PrintStream out);

Which is called in the following method:

private static void initializeSystemClass() {
like image 44
cowls Avatar answered Nov 16 '22 17:11

cowls