• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Swimming up Stream

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..........................................................
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmmmmm........
So they <b>are</b> variables in System, <b>and</b> also Objects of their respective types???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic