File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Finding an object reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding an object reference" Watch "Finding an object reference" New topic

Finding an object reference

Simon Wellborne

Joined: Oct 03, 2003
Posts: 2
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

Joined: Jul 22, 2000
Posts: 9059
Is ObjectB a subclass of ObjectA?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
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.

For my latest books on Java, including Introducing JavaFX 8 Programming, see
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Simon Wellborne

Joined: Oct 03, 2003
Posts: 2
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.
I agree. Here's the link:
subject: Finding an object reference
jQuery in Action, 3rd edition