out in System.out.println()

System.out is initialized to null when the class is instantiated. This is set by the nullPrintStream() method in System.java, which just returns null.

When the JVM has initialized, it calls the initializeSystemClass() method. This method calls the native method setOut0() which sets the out variable to the appropriate value.

This may seem weird but it is a necessary operation for the following reasons:

  • out cannot be set statically to the value because System needs to be one of the first loaded classes (before PrintStream).
  • out must be final so that its value cannot be directly overridden by a user.
  • Since out cannot be set statically, and is final, we must override the semantics of the language using a native method, setOut0().

I hope that helps your understanding.

Leave a Comment