aspose file tools*
The moose likes Beginning Java and the fly likes  about System.out.printnln() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " about System.out.printnln()" Watch " about System.out.printnln()" New topic
Author

about System.out.printnln()

Chaitanya Guggilla
Greenhorn

Joined: Dec 11, 2008
Posts: 1
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


Chaitanya Guggilla
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Hello and welcome to JavaRanch.

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:

System.out.println(data)


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 ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Welcome to JavaRanch.

"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:

System.out.println();

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:

System.out.println();

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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38726
    
  23
Originally posted by Mark Vedder:
"out" is a static field (or instance variable) . . .


I presume "instance" is a misprint there?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Campbell Ritchie:


I presume "instance" is a misprint there?


D'Oh. Yes, a misprint as a result of answering JavaRanch posts at 2:00AM due to insomnia. I've corrected it above. Thanks for catching that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38726
    
  23
At least if you had insomnia you were awake when you answered; I am usually asleep. At least the higher centres of my brain are
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about System.out.printnln()