Solved

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

Posted on 2004-10-20
14
180 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 125 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now