my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Query about System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query about System.out.println" Watch "Query about System.out.println" New topic
Author

Query about System.out.println

Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
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

Joined: Feb 18, 2005
Posts: 162

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 ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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.


Mani
Quaerendo Invenietis
Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
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

Joined: Feb 18, 2005
Posts: 162
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

Joined: Mar 05, 2002
Posts: 229
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about System.out.println
 
Similar Threads
System.err and System.out
what is out in System.out.println()
what does this mean??
System.out.println( );
Field definition