1

I'm landed into doubt where System class is having static field in of InputStream type. InputStream is a abstract class. Java document says, System.in stream is already open and ready to supply input data.

Abstract class cannot be initialized until its implemented. But Input stream has been implemented by various classes like AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream etc. So when we use System.in, which class's object gets instantiated ? Or if not Does it creates any anonymous class ?

Please share your views ..!

Andrea
  • 109
  • 2
  • 10

1 Answers1

3

The class of the object that is associated with System.in is a subclass of InputStream. Nothing more is specified1.

But we can test it:

$ cat Test.java
public class Test {
  public static void main(String[] args) {
    System.out.println(System.in.getClass());
  }
}
$ javac Test.java 
$ java Test 
class java.io.BufferedInputStream
$ 

Of course, this could vary from one version of Java to the next. The above is for Java 8.


1 - Since the class used for System.in is not specified, it is inadvisable to write code that depends on the empirical observation that the same class always seems to be used. However, I would not expect the people who provide Java implementations to change this detail ... because of the possibility that people could have hardwired knowledge of the class into their code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216