SCJP 1.6
SCWCD 1.5 (Preparing...)
There is still just ONE Cat object. But now TWO remote controls (references) can access that same Cat object.
So now, anything that B does to the Cat, will affect the Cat that A refers to, but it won't affect the A cup!"
Joanne
Campbell Ritchie wrote:
If however you write "B = new Cat();" it means create a new Cat object (maybe black cat called Tibbles at memory location 2345bcde) and use that instead of "B"
campbell@queeg:~/java$ javac CatDemo.java
campbell@queeg:~/java$ java CatDemo Tiger tabby
Before calling doStuff: Cat "A" = A tabby cat called Tiger No 1, which wants some dinner.
Hiss! Spit! WaaaaaaaaaH!
In doStuff method, before reassigning "B":
B = A tabby cat called Tiger No 1, which wants some dinner.
Hiss! Spit! WaaaaaaaaaH!
Hiss! Spit! WaaaaaaaaaH!
In doStuff method, after reassigning "B" to "new Cat()": black Fluffy
B = A black cat called Fluffy No 2, which won't eat even prime salmon now.
Prrrrrrrrrrrrrrrr
After calling doStuff: Cat "A" = A tabby cat called Tiger No 1, which wants some dinner.
Hiss! Spit! WaaaaaaaaaH!
The Cat which was previously referenced by "A" has not changed. "A" still refers to the same Cat as before.
No, you cannot have one reference to two objects simultaneously.
At the beginning of the doStuff() method you have both "A" and "Cat B" referring to the same object. Then later on, you change what "B" refers to.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
You start off with B pointing to the same Cat that A points to. Fred called it Fluffy, I called it No 1. A points to Fluffy/No 1. The Cat in the memory location which A points to is Fluffy/No 1.Vonique Leary wrote:When you say you change what B refers to, this is really A, correct, because A is passed into that method? So, while A is the reference to the new Cat, what is referring to the Cat that A used to point to?
(Inside this method, the reference 'A' doesn't really exist, or at least you can't really get to it, although the OBJECT it points to still does)
But the "A" reference in the other method still points to Fluffy/No 1.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Can I have a drink, too, please?Vonique Leary wrote:When you say "the other method" does this mean that A in the first method call is still referring to Fluffy, but A in the second call now points to the other cat?
If I were a drinker, I'd be getting the wine out by now.
SCJP6
Campbell Ritchie wrote:Another analogy. . .
In the next post.
SCJP6
No, but I soon shall beJason Irwin wrote:Are you psychic?
![]()
Campbell Ritchie wrote:Imagine you give me a piece of paper with your phone number copied on it. Then Rob Prime, in a moment of naughtiness, swaps the paper for one with Joanne Neal's phone number. So I pick up the paper, ring that number, and say, "Hello, Vonique."
"Whom are you calling Vonique? I'm called Joanne."
SCJP6
Yes, A in the first method still refers to Fluffy and you pass the number for A to the other method and now call it B. At that point both point to Fluffy. Then you change B so it points to a new Cat. But the original A still points to Fluffy.
I though you were buyingVonique Leary wrote:But for now, where's the wine???
You're welcomeThanks everyone!
Joanne
After the dostuff method, the reference variable is dead so that object is gc'd because the bit pattern is gone.
But the A's bitpattern is still there means the object is still be reached from the program.
SCJP6
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Don't get me started about those stupid light bulbs. |