This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instantiation again

 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys,

So Mammal human =new Mammal, what if I create another variable that points to the same object as human....So Mammal person; Now I don't want to just simply assign it like person = human, because the person will just copy the reference. Since human refers to some memory space that is storing a reference to this Mammal object, I want person variable to to refer to the same memory space that human is referring to. So basically, I want person to refer to the original reference, not simply store a copy of the reference. Because if human and person both refer to the original reference, by making one of them point to a new object , ex. person = female (female is pointing to some Mammal object), human should also point to the object that female is pointing at, which wouldnt be possible if person was just storing a copy of the reference, in which case person will point to a new object but human will point to the original object.
I hope I am clear enough....
Thanks
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm slightly confused, but let me see if I can answer.

If you assign human by doing human = person, they both have their own references to the same object. I don't know of a way for you to have person point to a different object and automatically have human point to it too, since person and human each hold their own reference to the mammal object, and when you reassign person it has no affect(effect?) on human. Perhaps someone else has an idea, but the best I can come up with is having a method that is called to set person to a new reference that then sets human to the same reference as person.

Why do you want this functionality? Maybe if you explain the thought process we can come up with a better solution.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you're describing requires direct access to pointers, which Java does not support.

What exactly are you trying to accomplish? If "person" should reference whatever object "human" is referencing (even when human is reassigned to point to a different instance), then what is the reason for having a separate variable, "person"? Why not just use "human"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic