How System.out.println
method works in both static and non static context?
Is there any JVM specific implementation is the answer for this?
How System.out.println
method works in both static and non static context?
Is there any JVM specific implementation is the answer for this?
System.out
is a static field / global object / singleton.
It is accessible everywhere, no need for a non-static context.
On that object (a PrintStream
instance) you can call the println
method.
Note that PrintStream#println
is not a static method. But you can call non-static methods even from static methods as long as you have an instance to call them on (such as System.out
here).
Static vs non-static context only makes a difference if you need access to this
(the current instance of your own class). That is not needed here (unless you want to do System.out.println(this)
-- that would not work from inside a static method).
None of this is "special" about System.out
or requires internal JVM magic.