• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.in an object ...how ??

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21004
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Justin Russo
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, got it.... Thanks a lot ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic