aspose file tools*
The moose likes Beginning Java and the fly likes System.out.println() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.println()" Watch "System.out.println()" New topic
Author

System.out.println()

Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
Hi all,
I am just learning Java and I understood that the data elements and methods of a class can be access using the dot notation. I'm confused about System.out.println(), I know that System is the class and println() is the method but where does "out" fit in? Is this a data member that access a method under the same class? Help pls =) Thanks!
Ilya PA
Greenhorn

Joined: Sep 05, 2001
Posts: 15
Hi, Margaret. It may sound a bit strange, but i think that 'out' is an object of 'System' class, and 'println()' is the method of 'System' class. We cannot say to a class to print(because a class is just a description of an object) but can say it to the object. But I'm not sure where the 'out' object was created, though...
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
Posts: 282
Hey,
Its actually much simpler than you might be thinking...
Actually System is one class having PrintStream's class's instance (named 'out') as public static member (you can have other class instances/objects as member variable to your class).
now println() is method of PrintStream's class, and its accessible through its object (named 'out' here).
now when you write System.out, its permitted as 'out' here is public static and than you write System.out.println() which is again alright as println() is public method of 'out' (PrintStream class).
Hope am clear.
Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
Hey Amit thanks a lot!
Why can't we make it easier by just accessing the PrintStream method directly from the class? and not having to go through all this? Is there any other way to print to the screen directly without having to pass by the System class? Like why can't we just use something like PrintStream.println() since println() method is directly from the PrintStream method, or create an object of the class PrintStream then access the println through this object?
Like for instance
PrintStream printSomething = new PrintStream();
printSomething.println();
Will this work? What is wrong with doing this?
why do we have to pass through the System class and then the instance variable from out from the class PrintStream to access the println method?
I'm sorry if I make you more confused =) If i am able to fully grasp this basic idea then it would be easier for me to understand the other methods which does this type of accessing. Thanks a lot!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
out is The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output. Otherwise you have to "open" and "close" the stream (at the very least). System.out.print() is already set up so you avoid lots of extra steps merely by typing an extra word.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
Posts: 282
ur welcome.
yes there is way to print on the screen (console) without using System class but than I think we might need to move this discussion to some advanced forum.
but just to give you an idea, you must have noticed that in PrintStream class, All constructors take at least one parameter i.e. an objcet of OutputStream.
so you can't do just like this..

PrintStream printSomething = new PrintStream();

you will need to pass an output stream object here. now in system class there is some native method call to get hold of (handle/pointer or by watever name you might like to refer) console (i.e. urs monitor/v.d.u.).
now if you intend to use your printstream object for printing onto screen, you will need to this (dirty) work and than your code becomes platform dependent (or supply that native method for each o.s. as Sun does).
here, you might think that why would you supply that native method code, instead you would simply use the native code/method that sun has used for assigning console to PrintStream. Right but than I assume its waste to even make a printstream object at first place and better to use the object available in System class. isn't it ?
hope am able to explain.
regards.
amit.
(at last, you might use this in case your intention is just to avoid using System...
PrintStream ps = System.out;
ps.println();
)
Originally posted by Margaret Tan:
Hey Amit thanks a lot!
Why can't we make it easier by just accessing the PrintStream method directly from the class? and not having to go through all this? Is there any other way to print to the screen directly without having to pass by the System class? Like why can't we just use something like PrintStream.println() since println() method is directly from the PrintStream method, or create an object of the class PrintStream then access the println through this object?
Like for instance
PrintStream printSomething = new PrintStream();
printSomething.println();
Will this work? What is wrong with doing this?
why do we have to pass through the System class and then the instance variable from out from the class PrintStream to access the println method?
I'm sorry if I make you more confused =) If i am able to fully grasp this basic idea then it would be easier for me to understand the other methods which does this type of accessing. Thanks a lot!


[This message has been edited by Amit Agrawal* (edited September 17, 2001).]
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
And actually "System" is short for the class "java.lang.System"...
so you could even write
<PRE>
java.lang.System.out.println("something");
</PRE>
... if you really wanted to!!!
Geoffrey

Sun Certified Programmer for the Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.out.println()