How does System.out.println internally works?
By looking at the Java statement we can easily infer that:
- System is a class.
- out is a static instance variable of a class which has a method println()
When and where is the out instantiated in System.out.println()?
When the JVM is initialised, the method initializeSystemClass() is called that does exactly what it's name says - it initialises the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable - this method is called setOut().
Here is what the different pieces of System.out.println() actually look like:
class System{ public static final PrintStream out; // . . . } //the PrintStream class belongs to java.io package class PrintStream{ public void println(); // . . . }