• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about System.out.println

 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know about System.out.println(), As far as my knowledge is concerned, System is a static and final class of java.lang package, out is also a static variable representing PrintStream object and println is a method associated with PrintStream to print output.. System.out represents an object of console. But I am confused about my last statement, System is static class, out is static variable representing PrintStream, the how come System.out is an Object. Will someone please clarify this thing.
Thanks in advance.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System is static class

System is a final class, but not static.

And about System.out :

The "standard" output stream. This stream is already open and ready to accept output data.


So, I don't see any problem here.

how come System.out is an Object.


System.out is a reference static variable which refer to instance of PrintStream class. That instance is prepared for you by your JVM.
[ July 07, 2005: Message edited by: George Bolyuba ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A correction there. System is a final class, but not a static class. (Any top-level class cannot be static)

out in the System class, is a static reference to a PrintStream object. So, when you say System.out, what you actually get is a reference to a PrintStream object (The PrintStream class contains methods like print(), println() etc.
 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for your clarifaction, But still its a bit confusive(for me), if System is not static class, then why is it taken as System.out, instead of making an object of System Class and then associating out with it? Please put more light on this.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abrahim, let me put it this way:

First, top-level class cannot be declared as static.

Second, image that you could create an instance of System class. Still you have only one system on which your current code is running. So, every System object will represent the same system. What's the point?

Is it clear now or I just didn't get yout problem?
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System is a class with static member variables. It is not a static class. A static class can only be defined within another class.

out is a public static final member variable of the System class. This means you can address it without having to create an instance of the System class. It will be initialized for you. It cannot be overridden or altered because it is final.

Hope this clears things up for you.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abrahim Daver:
Thanx for your clarifaction, But still its a bit confusive(for me), if System is not static class, then why is it taken as System.out, instead of making an object of System Class and then associating out with it? Please put more light on this.


This is because out is static. It has nothing to do with whether System is static or not (which it isn't as others have pointed out).

HTH

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic