Is there an easy way to find the "parent" of an object?

I understand that I can find the parents of components by using getParent(). But how would you get access to the object that instantiated the "child"?

Is there a way to do this without actually passing the "parent" as a parameter into the constructor?

example psuedocode:

public ParentClass(){
  ChildClass c = new ChildClass();
}

public ChildClass(){
  ParentClass p = (ParentClass) getTheParentClass();  // should return the ParentClass that instantiated it
}
LVL 1
darkpegasus5Asked:
Who is Participating?
 
expertmbConnect With a Mentor Commented:
the above code is not having the static method, this is suggestion for the author,  he can write the static methods to get the parent.

the author code
public ParentClass(){
  ChildClass c = new ChildClass();
}

public ChildClass(){
  ParentClass p = (ParentClass) getTheParentClass();  // should return the ParentClass that instantiated it
}

there is no other way you can get the parent (it should be the class which instantiated, this word is misleading) class.
you have to set explicitly or pass as a parameter to constructor.
0
 
Tommy BraasCommented:
Hi darkpegasus5,

You would only be able to do that by instrumenting the classes you would like to track. You would instrument them such that the stack trace of the calling thread is either saved or processed such that the instantiating object is saved as local state.

Short version: you have to do it yourself

\t
0
 
msmolyakCommented:
The parent classes can be interpreted in at least on of three ways. I would like to understand what you have in mind.

In Swing the GUI is composed out of components some of which are containers. If a button is added to a panel, the panel is the parent of the button and can be obtained by calling getParent() method.

Parent a child classes may mean the two classes, the latter extending the former.

The third meaning deals with class dependencies. If class A instantiates class B, the class A is the clinet of class B and dependent on it (you may call it a parent although it is not a common terminology).

Could you now clarify your question?

Michael
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
darkpegasus5Author Commented:
I think that 3rd option may be more of what I am getting at. Maybe it would help if I explained exactly what I was trying to do.

I have Class A, in which I declare a new instance of Class B and Class C.

Class B needs to get data to Class C, but they aren't related. I'd like Class B to go through Class A to send data to Class C and vice versa.

  A
 / \
B  C
0
 
Tommy BraasCommented:
darkpegasus5,

I thought you were doing some kind of depency analysis/debugging.

You basically have two options; You would have to implement that functionality yourself, or use the PropertyChangeSupport class.

Here's the link for PCS http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyChangeSupport.html

Very simple to use, create a PCS, register listeners, fire an event when a property changes.

\t
0
 
expertmbCommented:
public class A {

      private String str = "";
      private B b;
      private C c;


      public static void main(String[] args) {
            A a = new A();
            a.b.setForC();
            a.c.setForB();
            System.out.println(a.c.get());
            System.out.println(a.b.get());
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return str;
      }

      public void setForC(String str){
            c.set(str);
      }

      public A(){
            b = new B(this);
            c = new C(this);
      }

      public void setForB(String str){
            b.set(str);
      }
}


class B {
      A a;
      String str = "";

      public B(A a){
            this.a = a;
      }

      public void setForC(){
            a.setForC("From B to C");
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return this.str;
      }
}


class C {
      A a;
      String str = "";
      public C(A a){
            this.a = a;
      }

      public void setForB(){
            a.setForB("From C to B");
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return this.str;
      }
}
0
 
zzynxSoftware engineerCommented:
expertmb, the question was:
>>Is there a way to do this without actually passing the "parent" as a parameter into the constructor?
0
 
zzynxSoftware engineerCommented:
>> I'd like Class B to go through Class A to send data to Class C and vice versa.
Why should that go through class A? Why not directly via the appropriate setter functions?
0
 
expertmbCommented:
hmmm not seen that ,

modified source
you can use some static methods

public class A {

      private String str = "";
      private B b;
      private C c;


      public static void main(String[] args) {
            A a = new A();
            a.b.setParent(a);
            a.c.setParent(a);
            a.b.setForC();
            a.c.setForB();
            System.out.println(a.c.get());
            System.out.println(a.b.get());
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return str;
      }

      public void setForC(String str){
            c.set(str);
      }

      public A(){
            b = new B();
            c = new C();
      }

      public void setForB(String str){
            b.set(str);
      }
}


class B {
      A a;
      String str = "";

      public B(){
      }

      public void setForC(){
            a.setForC("From B to C");
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return this.str;
      }

      public void setParent(A a){
            this.a = a;
      }
}


class C {
      A a;
      String str = "";
      public C(){
      }

      public void setForB(){
            a.setForB("From C to B");
      }

      public void set(String str){
            this.str = str;
      }

      public String get(){
            return this.str;
      }

      public void setParent(A a){
            this.a = a;
      }
}
0
 
zzynxSoftware engineerCommented:
>> you can use some static methods
Don't see any static method in your code

Of course a function

     public void setParent(A a)

is not "passing the parent as a parameter into the constructor", it's just "passing the parent as a parameter" ;°)
But it would amaze me if the author meant that.
0
 
darkpegasus5Author Commented:
You are correct, calling it a parent is misleading in java terms. It is the class which instatiated, but that takes longer to write and I hoped I had explained it better :-)

Okay, so let's say I create a static class, or at least a class that has some static methods. This would easily remedy some of the passing parameters around about the "class that instantiated". However, if I am running the code on a server with multiple instances of say, a Game class, is there a way to isolate that particular instantiated class?

Here's what I would like to do:

There is a game server with two seperate instances of a Game class (gameA & gameB). Each Game has two instances of a CardsInHand class. The CardsInHand class calls a static method in the Game class called getNextCard().

If a CardsInHand object in gameA calls the static getNextCard() method of the Game class, will it necessarily get the results from gameA? Will it sometimes get the results of gameB? Will both gameA & gameB methods be called and attempt to return a value? Or is there some other "weird" behavior that may happen?

Sorry if these sound like rudimentary questions, but thanks for your help.
0
 
Tommy BraasCommented:
darkpegasus5,

If you use the PropertyChangeSupport described above, you won't even have the problem you're describing.
0
 
darkpegasus5Author Commented:
Do you have an example of how to use it?
0
 
expertmbCommented:
if more than one thread is accessing the piece of code/data then making static blocks is not the best opton. if the data is not thread safe you can make the block of code synchronized, which will slow your application.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.