Page 318 of Java 2: The Complete Reference states that System contains 3 predefined stream variables, in, out, and err. Further down the page it states that System.in in an object of type InputStream; System.out and System.err are objects of type PrintStream. How can in, out, and err be both variables and objects??? I are confused..........................................................
The book may have been clarifying that the variable (more correctly, "object reference") is of type X and so is the object since it is possible that they are different.
Here is an example where they are not the same:
The reference is of type java.lang.Object. The object is of type java.lang.String.
The reference is of type java.io.InputStream. The object is of type java.io.FileInputStream.
However, in the case of the standard input stream (which is represented by an InputStream), this cannot be the case, since an object cannot be of type InputStream (or any abstract class/interface). </speculation>
Okay, so they aren't actually both a variable and an object. System.in/out/err are static variables declared inside their respective classes such as InputStream and PrintStream (did I get those right?) and those variables reference objects of their respective types??? Oyyy I have such a headache!
author and iconoclast
Saying that a variable "is an object of type X" is a kind of shorthand; sloppy, maybe, but generally understood.
So to be precise, System.in is a variable of type InputStream, which references an object of type SecretInputStreamSubclass (in EFH's example). Speaking more lazily, we might say that System.in "is" a SecretInputStreamSubclass. That's a useful shorthand in conversations where we don't need to distinguish between the declared type of a reference, and the actual type of the referenced object. But when ambiguity arises, it becomes necessary to speak more precisely: System.in is a variable of type InputStream, and it references an object of type SecretInputStreamSubclass.
"I'm not back." - Bill Harding, Twister
Joined: Mar 15, 2001
Okay, I think the light bulb is beginning to glow a little........ They are variables declared inside of System, and they reference objects of whichever type they reference, but they are variables. (Am I getting warmer?)
You're getting there. I don't like the word "Variable" any more. It's sort of out of date. "Placeholder" is sort of a better word.
Think about it this way, and I'm going to go in small steps, not because I think you can't handle it, but because I want to make sure I don't assume something.
Every variable holds something. Substitute the word "variable" for "placeholder" and it might make more sense.
Some variables hold basic types: long, boolean, int, they all hold a basic type. Some variables hold objects: StringBuffer, Integer, Object. Anything that holds an object is derived from Object. (Note that arrays are objects, but forget that, it's just complicating the issue.)
A CLASS is the definition of an object. An object is the instantiation of a class.
If I have:
then I can write
If you said that b2 holds an object of type Derived, you'ld be correct. Same if you said it holds an object of type Base. Same if you said it holds an object of type Object.
I know I had a hard time wrapping my head around this concept. Here is a fragment of the java.lang.System class.
Remember that when a variable is static, it is referenced by the name of the class rather than by an object of the class. Because System.out is an object, it has methods.
Here is a different (very incomplete) example where a dog uses the mouth variable that is defined in the Animal class.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Mar 15, 2001
Robert, thanks for the explanation. It really helped. Small steps are what I need. I don't have a background in programming, so the basics that one might assume everyone understands can be a real sticking point for me. I think i'm starting to get it. Marilyn, I have 2 questions. 1)Because mouth is declared static inside Animal, there is no need to create an object of type Animal to use the method openAndClose(), but if I wanted to use walk(), I would have to instantiate an object of type Animal first, right? 2)If I were to access openAndClose() from the class Mouth, I would need to create an object of class Mouth first, right? These are examples of inheritance and the static keyword and the different ways they can be used, right?