• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call method from an object that is contained in another object

 
Jiangbo Tang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic