aspose file tools
The moose likes Beginning Java and the fly likes Help on object references understanding... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Help on object references understanding..." Watch "Help on object references understanding..." New topic
Author

Help on object references understanding...

Wee Keong Soh
Greenhorn

Joined: Apr 08, 2009
Posts: 18
Hi,

class Dog {
Cat c = new Cat();
}

class Cat {
//blah blah blah
}

Dog d = new Dog();
Dog a;
a = d;

im aware that the Dog object will have 2 references refering to it. But how many references to Cat object will i have?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
One !!!

Two references, both pointing to the same single object.
Wee Keong Soh
Greenhorn

Joined: Apr 08, 2009
Posts: 18
Hi,

So am i right to say that there will be 1 dog object with 2 references to it, and 1 cat object also with 2 references to it?

Thanks
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
No.

1) 1 Dog object with 2 references to it - correct
2) 1 Cat object with 2 references to it - wrong

The cat object has only 1 reference, that is the dog object.
Wee Keong Soh
Greenhorn

Joined: Apr 08, 2009
Posts: 18
ok thanks.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Help on object references understanding...
 
Similar Threads
Seeking Exam Advise
casting reference
Help with assigning references
ClassCastException
A curious case of instanceof keyword usage in JDK 1.5