| Author |
System.out.println Explanation
|
Deepika Saxena
Ranch Hand
Joined: Jul 05, 2009
Posts: 59
|
|
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
Joined: May 13, 2009
Posts: 679
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: System.out.println Explanation
|
|
|