Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding an object reference

 
Simon Wellborne
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have typed the following question 3 times in an effort to get it making sense.
What I want to know is this: Is it possible for an instance of objB to find out what instance of an objA is referencing it?
In particular, I want to be able to call a method from the referencing objA from objB. (Gee, I hope all that makes sense?!).
Just in case I have not explained myself using correct terminology, maybe this might help?
objA has instance variables of type objB.
class ObjectA {
ObjectB myobj = new ObjectB();

public void mymethodA(){

}
}
class ObjectB {
public void mymethodB(){
// Needs to call mymethodA of the referencing objA
}

}
Appreciate any help.
[ October 03, 2003: Message edited by: Simon Wellborne ]
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is ObjectB a subclass of ObjectA?
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon: As you frame your question ("Is it possible for an instance of objB to find out what instance of an objA is referencing it?") and in conjuction with the code snippet you show, the answer is No. There is no mechanism in Java to allow this as you describe it. Thus, ObjectB can't call mymethodA() **just because** ObjectA has an instance variable of type ObjectB. ObjectB has no knowledge of ObjectA. Nor does an object of type ObjectB have knowledge of who might be holding a reference to it.
To the larger issue that underlies your question, what you seem to be trying to accomplish appears to require inheritance, in which ObjectB is a subclass of ObjectA. This is what Marilyn is getting at by her question. If ObjectB is derived from ObjectA, then ObjectB can call non-private methods defined by ObjectA.
If ObjectA and ObjectB must be separate classes, then you need to have ObjectB hold a reference to an object of type ObjectA, probably setting this reference in ObjectB's constructor. Then, you could call mymethodA() through the ObjectA reference held by ObjectB.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a "call back" kind of situation. Sometimes you ask another object to do some work knowing that it will have to ask you for more information along the way, or maybe report the results back to you later.

That's horribly abbreviated - not real Java - hope it made sense. The "finished" kind of message usually makes more sense if youre running on separate threads.
Anyhow, the bottom line is B has to have a reference to A if it's going to call a method back on A. This is a little risky as you have circular dependencies, objects that depend on each other in ways we'd rather not. As a more advanced technique, we might make them both depend on interfaces.
 
Simon Wellborne
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, and thanks to all who responded to my question.
Herb - thanks in that you answered what was my real question (Is there a mechanism to do this). I might go check out some of your books :-)
Stan - I had contemplated passing the objA reference to the objB constructor as a means of linking the objects, but I like your approach.
Cheers all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic