how the System.out.println works?

System is a class and out is a static reference variable in the system class that will gives the reference for the printstrem class to access println method. How the printstrem object is created and gives referance to the out static variable.

Jon Skeet
people
quotationmark

System.out is a bit special. Even though it's a final field, it's manipulated by native code - which is how System.setOut is able to work.

When the System class is initialized, System.out is initialized in native code to a reference to an appropriate stream which will write to the console.

In the JDK 7 code that I'm looking at, there's a private static void initializeSystemClass method which is executed by the VM after the normal static initializers. That contains:

FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));

... where setOut0 is a native method which will change the value of System.out.

people

See more on this question at Stackoverflow