• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about System.out.printnln()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic