Hi Guys, I need Clarification about System.out.println() . Here println() Method belongs to PrintStream class ,we can call a non static method by using an Object of that particular class. Here why we are not creating PrintStream class Object and How System,out,println() are related
You might very well have got the answer if you would have looked into the J2SE docs. I have done a part for you.
This comes directly from the API docs.
out public static final PrintStream out
The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user. For simple stand-alone Java applications, a typical way to write a line of output data is:
See the println methods in class PrintStream.
Do the rest of it on your own.
Hope this helps. [ December 12, 2008: Message edited by: Sudipto Shekhar ]
"out" is a static field of the System class. "out" is a PrintStream. Put another way, the out variable is a reference to an object of type PrintStream.
You do not have to create it because it is automatically created by the System class the first time the System class is referenced. So all we need to do is say:
and we can print things to the console (i.e. the screen).
Now although println is a non-static method, we are not calling it on a Class directly. Entering PrintWriter.println() would not work because we would then be calling println() -- a non-static method -- statically.
Put another way, doing this:
is the same as if we were to do this:
PrintWriter pw= System.out; pw.println();
It is just more convenient to do it all in one line. This is called Method Chaining. [ December 12, 2008: Message edited by: Mark Vedder ]