Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.println Explanation

 
Deepika Saxena
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic