• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about System.out.printnln()

 
Chaitanya Guggilla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Vedder:
"out" is a static field (or instance variable) . . .


I presume "instance" is a misprint there?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic