aspose file tools*
The moose likes Beginning Java and the fly likes about system.in & system.out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about system.in & system.out" Watch "about system.in & system.out" New topic
Author

about system.in & system.out

kanagaraj swamy
Greenhorn

Joined: Dec 18, 2000
Posts: 8
hi
when i read a book it was given that "in" & "out" in system.in & system.out are variables but we can acess methods only through objects so how the method print() is called by the variables in & out.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
in and out are variables that reference objects.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The methods are static methods, and therefore do NOT require an object in order to use them.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Kalidas Pavi
Ranch Hand

Joined: Nov 20, 2000
Posts: 42
Kanagaraj,
Let me try to explain a little bit on this. System class contains the static fields
public static final PrintStream out
public static final InputStream in
public static final PrintStream err
which refers to the standard output, input and error streams. note that these fields are declared as static and hence can be accessed without instantiation.
print() and println() are methods defined in the PrintStream class. out being defined as Printstream type can access println() using out.println().
Hope this is clear.
Kalidas
Ryan Tracy
Ranch Hand

Joined: Mar 21, 2001
Posts: 33
Can you give me cases of when you would use each of these static methods?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Kalidas I think you'll find they are not final. There are setters for them so you can plug in your own.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Stan James:
I think you'll find they are not final.


Ah. This is one of the hairy little corners of the Java API. They are final -- go check! The setOut(), setIn(), setErr() methods delegate to native methods, which aren't bound by ordinary mortal Java rules. Final variables which can change -- in the API! Aaaak!

A better implementation seems obvious to me: PrintStream isn't final, so they could have just used a package-private PrintStream class which allowed the underlying OutputStream to be changed, and then have setOut() merely twiddle that internal setting, without this crazy final variable modification.

Never did figure out their rationale there.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Cindy Glass:
The methods are static methods, and therefore do NOT require an object in order to use them.


The print() and println() methods are NOT static, but as mentioned above, you call them via static reference variables. This means that you are calling these methods on objects just as required for non-static methods.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about system.in & system.out