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

System.in an object ...how ??

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Hello,

import java.lang package defines a class called System.
System contain 3 pre-defined stream variables in,out and err which are declared as public and static within System.

We know System.out refers to standard output stream and likewise System.in refers to standard input stream.

Now the problem is with these 2 statements below -

1. System.in is an object of type InputStream.
2. System.out and System.err are the objects of type PrintStream.

Since 3 variables are static they can be called by class name.(like System.in etc) but how come System.in can be an object. in is a variable of System class and likewise out & err.
[ April 05, 2008: Message edited by: Shivit Agarwal ]

Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Since 3 variables are static they can be called by class name.(like System.in etc) but how come System.in can be an object. in is a variable of System class and likewise out & err.


in, out, and err, are static variable references of the System class. References, of course, refer to objects. So... with System.in, the compiler will use the object that is referred to, by the in reference of the System class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
System.in is an variable but is a type of InputStream.

so it will ultimately refer to an object of class InputStream.

So is the case for other variable.



Where did you see these statements ?


You Want it.. Get it.......the Right Way...<br /> <br />SCJP 5.0 SCWCD 5.0
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Oh Sorry ,I did such a silly mistake. But still good , learned something.

I mistook(thought) the variable "in" as instance variable of System class.

Thank you above's . Thanks a lot.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Another question just popped up. Why is that reference variable declared as Static ??

What is the advantage of declaring objects or reference variable as Static ??

Is the reason same as declaring Static to a method and instance variable Or is their is any other purpose served declaring objects as static. (I haven't yet written any code declaring a object or reference variable as static.)
Justin Russo
Ranch Hand

Joined: Oct 21, 2007
Posts: 77
Static variables by definition are class variables i.e they have one one copy shared across all object instances of that class.

Why you need static Variables?
Thats because when you designed your class you need a variable which should keep only one copy of itself for eg.

Say you needed a utility class which would do a lookup based on the country code you passed and returned the Region to which it belongs..



So as you see in the above case there is no need for creating a instance variable when static variable served the purpose.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Ok, got it.... Thanks a lot ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.in an object ...how ??