6

Following pseudocode sums up my question pretty well I think...

class Owner {
    Bar b = new Bar();

    dostuff(){...}
}    

class Bar {
    Bar() {
        //I want to call Owner.dostuff() here
    }
}

Bar b is 'owned' (whats the proper word?) by Owner (it 'has a'). So how would an object of type Bar call Owner.dostuff()?

At first I was thinking super();, but that's for inherited classes. Then I was thinking pass an interface, am I on the right track?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
jason
  • 4,721
  • 8
  • 37
  • 45

8 Answers8

10

If dostuff is a regular method you need to pass Bar an instance.

class Owner {

   Bar b = new Bar(this);

   dostuff(){...}
}    

class Bar {
   Bar(Owner owner) {
      owner.dostuff();
   }
}

Note that there may be many owners to Bar and not any realistic way to find out who they are.

Edit: You might be looking for an Inner class: Sample and comments.

class Owner {

   InnerBar b = new InnerBar();

   void dostuff(){...}

   void doStuffToInnerBar(){
       b.doInnerBarStuf();
   }

   // InnerBar is like a member in Owner.
   class InnerBar { // not containing a method dostuff.
      InnerBar() { 
      // The creating owner object is very much like a 
      // an owner, or a wrapper around this object.
      }
      void doInnerBarStuff(){
         dostuff(); // method in Owner
      }
   }
}
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • @jason. If owner is an important concept for Bar, you might look in to the Inner class others have mentioned. This basically would make Bar a member in Owner. – Captain Giraffe Feb 15 '12 at 20:12
  • I dislike that this (like all answers so far) is tight coupling: `Bar` should not know about the `Owner` type except for that one function (declaration) it wants to talk to. Reusing `Bar` elsewhere gets complicated this way. Not sure how a simple "event property" aught to be done in JAVA though, it may need a bunch of Listeners code. – Barry Staes Aug 19 '15 at 09:50
3

This would work:

class Owner {

    Bar b = new Bar(this);

    dostuff(){...}
}    

class Bar {
    Bar(Owner myOwner) {
        myOwner.dostuff();
    }
}
Costique
  • 23,712
  • 4
  • 76
  • 79
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
3

I think you are looking for nested Clases Nested Classes Sun

This way u can write outer.this.doStuff();

Have a look to that topic: Inner class call outer class method

Community
  • 1
  • 1
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
2

In the way you're putting it, there is no way of calling the "owner" in Java.

Object A has a reference of object B doesn't mean that object B even knows that object A exists.

The only way to achieve this would be either though inheritance (like you said yourself), or by passing an instance of object Owner to the constructor of Bar.

pcalcao
  • 15,789
  • 1
  • 44
  • 64
  • "object B doesn't mean that object B even knows that object A exists" - Ive always thought this was odd. Some things work when theyre mutually aware, but I need to remember that this is not the default way it works. Was also secretly hoping for some mystical java-fu that I missed. – jason Feb 15 '12 at 20:10
2
class Owner {
    Bar b = null;
    Owner(){
       b = new Bar(this);
    }
    dostuff(){...}
}    

class Bar {
    Owner o = null;
    Bar(Owner o) {
        this.o = o;
    }
}

Now, instance b of Bar has a reference to o of type Owner and can do o.doStuff() whenever needed.

GETah
  • 20,922
  • 7
  • 61
  • 103
1
class Owner {

    Bar b = new Bar(this);

    dostuff(){...}
}    

class Bar {
    Bar(Owner owner) {
       owner.doStuff();
    }
}
david a.
  • 5,283
  • 22
  • 24
1

I think the way you have written the code, it is not possible to do. But if you declare Bar as inner class of Owner, you might get a closer solution.

Bhushan
  • 18,329
  • 31
  • 104
  • 137
1

There are 3 possibilities :

1) making dostuff() static and call it like

Owner.dostuff()

2) Creating an instance of Owner inside the class Bar

class Bar {
   Owner o;
   public Owner() {
     o = new Owner();
     o.dostuff();
   }
}

3) Inject an Owner instance through the constructor

class Bar {
   public Owner(Owner o) {
     o.dostuff();
   }
}
Don
  • 1,134
  • 8
  • 15