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 becauseSystem
needs to be one of the first loaded classes (beforePrintStream
).out
must befinal
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 anative
method,setOut0()
.
I hope that helps your understanding.