1

In java, java.lang.System class, which has an in static variable.
Declared as: public static final InputStream in
Which means that in is an InputStream varibale.
However I see some example, using System.in.read() to read input.



How can it do that, the read() method in InputStream is not a static method, how can it be directlly call it? As my understanding, only static method can call directly by a Class without creating an instance.


read() declartion: public abstract int read() throws IOException


Thank you Jon Skeet for answering, I still have some point don't understand.

If I call System.in.read() which mean that I call InputStream class method read() ?

java.lang.System.in -----> java.io.InputStream ----> read()
  1. calling java.lang.System.in ( which is a static variable), in is a java.io.InputStream variable
  2. The calling act like calling PrintStream class.
  3. The process work like calling : PrintStream.read()
  4. however I have difficulty on understand the read() method, which is not a static method, as it’s should not be call directly.
  5. It should be call like:

    PrintStream rd = new PrintStream(); int c = rd.read();

as read() should be call by an instance. read() declartion: public abstract int read() throws IOException

PS: I try this code does not work:

InputStream rd = new InputStream();
        int c = rd.read();
        System.out.println(c);

But do not know why.

ref: http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

loadload
  • 11
  • 2

1 Answers1

7

How can it do that, the read() method in InputStream is not a static method, how can it be directlly call it?

You're calling it on an instance of InputStream, via the static in variable. So this:

int c = System.in.read();

is equivalent to:

InputStream stream = System.in;
int c = stream.read();

Does that help to make it clearer?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you Jon Skeet for answering, I still have some point don't understand. – loadload Aug 18 '13 at 17:14
  • @loadload: Well without specifying what that point is, I don't think I (or anyone else) can help you. – Jon Skeet Aug 18 '13 at 17:15
  • If I call System.in.read() which mean that I call InputStream class method read() ? java.lang.System.in -----> java.io.InputStream ----> read() 1.calling java.lang.System.in ( which is a static variable), in is a java.io.InputStream variable 2.The calling act like calling PrintStream class. 3.The process work like calling : PrintStream.read() 4.however I have difficulty on understand the read() method, which is not a static method, as it’s should not be call directly. – loadload Aug 18 '13 at 17:57
  • 5. It should be call like: PrintStream rd = new PrintStream(); int c = rd.read(); as read() should be call by an instance. read() declartion: public abstract int read() throws IOException PS: I try this code does not work: InputStream rd = new InputStream(); int c = rd.read(); System.out.println(c); But do not know why. – loadload Aug 18 '13 at 17:58
  • I edited my question, as in comment I don't know how to format the code. – loadload Aug 18 '13 at 17:58
  • @loadload: Your edited question suggests you haven't really read my answer at all. It *doesn't* acts as `InputStream.read()` - it acts as a call to `read()` on the value which has been read from a variable. (You don't "call" a variable - you read it or write it.) And you can't call `new InputStream()` like that because it's an abstract class. These are all fairly basic points - I suspect you should reread whatever book you're learning Java from, as Stack Overflow isn't really good for introducing concepts from scratch. – Jon Skeet Aug 18 '13 at 18:00
  • @loadload: It's not clear where you got the idea that `PrintStream` is involved, either. `System.in` is of type `InputStream`, not `PrintStream`. – Jon Skeet Aug 18 '13 at 18:02
  • Jon Skeet, Thank you so much for your explanation, I need sometime to digest it and I will reread the textbook. Again, thanks a lot. – loadload Aug 18 '13 at 19:05