Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-20
14
Medium Priority
?
187 Views
Last Modified: 2010-08-30
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
}
0
Comment
Question by:darkpegasus5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
14 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12365056
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 12365430
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
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12366273
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12366365
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
 
LVL 6

Expert Comment

by:expertmb
ID: 12366931
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12367041
expertmb, the question was:
>>Is there a way to do this without actually passing the "parent" as a parameter into the constructor?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367047
>> 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
 
LVL 6

Expert Comment

by:expertmb
ID: 12367082
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12367188
>> 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
 
LVL 6

Accepted Solution

by:
expertmb earned 375 total points
ID: 12367227
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
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12375633
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12377595
darkpegasus5,

If you use the PropertyChangeSupport described above, you won't even have the problem you're describing.
0
 
LVL 1

Author Comment

by:darkpegasus5
ID: 12417604
Do you have an example of how to use it?
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12418191
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question