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

1 Answer 1

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?

8
  • Thank you Jon Skeet for answering, I still have some point don't understand.
    – loadload
    Aug 18, 2013 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, 2013 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, 2013 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, 2013 at 17:58
  • I edited my question, as in comment I don't know how to format the code.
    – loadload
    Aug 18, 2013 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.