• 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

System.out.println Explanation

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a very small doubt in understanding the System.out,println(). I tried to search it within the Javaranch forumn but the search option is not allowing me to enter anything other than alphabets.
I know that System is a class and out is an object of PrintSteream type and println is a method of PrintStream.
Could any body please explain why this statement is designed in such a way? i.e we are accessing a static method through an object , not from a System class.
Also, please let me know whether this is according to Singleton Design pattern?
Thanks.
--Deepika
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepika Saxena wrote:Hi,
I have a very small doubt in understanding the System.out,println(). I tried to search it within the Javaranch forumn but the search option is not allowing me to enter anything other than alphabets.
I know that System is a class and out is an object of PrintSteream type and println is a method of PrintStream.
Could any body please explain why this statement is designed in such a way? i.e we are accessing a static method through an object , not from a System class.
Also, please let me know whether this is according to Singleton Design pattern?
Thanks.
--Deepika



Well, for starters, according to the API doc for java.lang.System, out is more than just an object of type PrintStream. it is the Standard Output Stream.

You may find thi article helpful.

http://www.javaworld.com/javaworld/jw-03-2001/jw-0302-java101.html
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepika Saxena wrote:Could any body please explain why this statement is designed in such a way? i.e we are accessing a static method through an object , not from a System class.


Because adding all those println statements would pollute the System class with many, many extra methods. Instead of the 26 current methods, there would have to be an additional 31 for the output, 31 for the error and 9 for the input. Also, the names for output and error methods would have to be mangled to get a difference. You would get methods like printToOutput and printToError.

Also, any method that takes an input stream or output stream could no longer read from the standard input stream or write to the standard output / error stream.


By having System.out, System.err and System.in, these remain instances of PrintStream and InputStream, and can therefore be passed to methods.
 
reply
    Bookmark Topic Watch Topic
  • New Topic