Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes call method from an object that is contained in another object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "call method from an object that is contained in another object" Watch "call method from an object that is contained in another object" New topic
Author

call method from an object that is contained in another object

Jiangbo Tang
Greenhorn

Joined: Dec 04, 2012
Posts: 2
Hi Guys,

Let's say I have a class Foo and class Boo. Class Foo has an array of reference variables that is of type Boo.

public class Foo {
Boo[] boo = new Boo[3];
public foo() {
boo[o] = new Boo();
boo[1] = new Boo();
boo[2] = new Boo();
}
}

public class Boo {
public void boodostuff () {
}
}

now suppose I put a main method in the class Foo and want to in the main call boodostuff from one of the boo object, how can I do that?

I have tried:
Foo foo = new Foo();
foo.boo[1].boodostuff();
which didn't work for me.

UPDATE: foo.boo[1].boodostuff(); actually worked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
It might work, but it is bad design. Your Foo class should keep quiet about its inside workings (which is called encapsulation). If the Foo class should be designed to allow access, it should be via one of its methods.Also google for Law of Demeter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call method from an object that is contained in another object
 
Similar Threads
Doubt related to casting
inner anonymous class fields initialization
problem in anonymous inner classes
method local inner class
instanceof