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.
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.
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
Joined: Feb 28, 2008
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.
Joined: Feb 28, 2008
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.)
Joined: Oct 21, 2007
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.