aspose file tools*
The moose likes Java in General and the fly likes Swimming up Stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Swimming up Stream" Watch "Swimming up Stream" New topic
Author

Swimming up Stream

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
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..........................................................
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

System.in is a variable of type InputStream that refers to an object, also of type InputStream. You can make similar statements about System.out and System.err .

Saying that a variable "is an object of type X" is a kind of shorthand; sloppy, maybe, but generally understood.


[Jess in Action][AskingGoodQuestions]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.

And another:

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>


Tony Morris
Java Q&A (FAQ, Trivia)
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
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!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Carol Murphy:
System.in/out/err are static variables declared inside their respective classes


No; they're declared inside System. The code might look something like



That's not so bad, is it?
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Hmmmmmmmm........
So they <b>are</b> variables in System, <b>and</b> also Objects of their respective types???
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Quoting EFH above:

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
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
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?)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yup.
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Carol,

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.

Did I make any sense?


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swimming up Stream