Dumb question: How do I get an object's parent?

Discussion in 'Java' started by El Guerrero del Interfaz, Jun 9, 2005.

  1. Hi,


    I know it's a dumb question but then I'm newbie, so sorry about
    that.

    class MyObject {
    anotherClass innerObject;
    }
    ..
    ..
    ..
    MyObject myObject = new MyObject();
    ..
    ..
    ..

    What I want to do is how, knowing innerObject, do I get a reference
    to the myObject instance that contains it? Is it possible?


    Bye and thanks.




    --
    El Guerrero del Interfaz
     
    El Guerrero del Interfaz, Jun 9, 2005
    #1
    1. Advertising

  2. "El Guerrero del Interfaz" <> wrote in
    message news:...
    > Hi,
    >
    >
    > I know it's a dumb question but then I'm newbie, so sorry about
    > that.
    >
    > class MyObject {
    > anotherClass innerObject;
    > }
    > .
    > .
    > .
    > MyObject myObject = new MyObject();
    > .
    > .
    > .
    >
    > What I want to do is how, knowing innerObject, do I get a reference
    > to the myObject instance that contains it? Is it possible?


    You put a reference to myObject inside of innerObject. The problem is that
    there's no such thing as "parent" object. innerObject could be referenced
    by any number (well, 1 or more) other objects, so if you want to go from
    innerObject to myObject you must store that association somewhere (in
    innerObject, in a separate object or index, by search, etc).

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Jun 9, 2005
    #2
    1. Advertising

  3. El Guerrero del Interfaz wrote:
    > Hi,
    >
    >
    > I know it's a dumb question but then I'm newbie, so sorry about
    > that.
    >
    > class MyObject {
    > anotherClass innerObject;
    > }
    > .
    > .
    > .
    > MyObject myObject = new MyObject();
    > .
    > .
    > .
    >


    I believe that code running in methods of the inner object can
    get at the outer instance by referring to "MyObject.this". e.g.

    mummy = MyObject.this;

    However, having a reference to the inner object doesn't help
    unless a method of the inner object contains this code. You
    really need the inner object's help in this respect.

    I would query the need to find the outer object unless the inner
    object is written to explicitly give you that information.

    Steve
     
    Steve Horsley, Jun 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LB
    Replies:
    5
    Views:
    603
  2. JJ
    Replies:
    0
    Views:
    139
  3. Elf M. Sternberg
    Replies:
    15
    Views:
    289
    Matthias Reitinger
    Jul 29, 2009
  4. Marc Wilson

    Dumb newbie question- object references

    Marc Wilson, Nov 25, 2003, in forum: Javascript
    Replies:
    4
    Views:
    153
    Marc Wilson
    Nov 26, 2003
  5. Jerry C.
    Replies:
    8
    Views:
    279
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page