This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
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.
Joined: Aug 06, 2004
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.
Joined: Feb 18, 2005
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?
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.
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).