Is this static println
function in out
class from System
namespace?
namespace System { class out { static println ...
System is the java class.
out is the instance and also static member of PrintStream.
println is the method of PrintStream.
System.out.println
System
is a class in the java.lang
package.
out
is a static
data member of the System
class and references a variable of the PrintStream
class.
• System
is a class in java.lang
package
• out
is a static
object of PrintStream
class in java.io
package
• println()
is a method in the PrintStream
class
It is quite simple to understand the question, but to answer it we need to dig deeper in to Java native code.
System
is static class and cannot be instantiatedout
is a reference variable defined in System
println()
is the method used to print on standard output.A brief and nice explanation is always welcome on this as we can learn much from this single line of statement itself!
From the javadoc about System
, here's what the doc says:
public final class System
extends Object
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Since:
JDK1.0
Regarding System.out
public static final PrintStream out
The "standard" output stream class Prinstream belongs to java.io package. This stream is already open and ready to accept output data.
When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s name says – it initializes 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().
Typically this stream corresponds to display output or another output destination specified by the host environment or user.
Regarding println();
class PrintStream{
public void println();
}
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data);
System.out.println()
High level Understanding
For understanding this we need to recall few basics of java:
Now with basic knowledge of java we know :
Lets get more in details:
out variable: static or instance?
called using class name, so we know its static variable of System class.
but its calling a method println() method so ‘out’ is an object of the reference type PrintStream.
the System class belongs to java.lang package
class System {
public static final PrintStream out;
//...
}
the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}