In Java, I can write something like this:
public class Brother {
private final Parent parent;
public Brother(Parent parent) {
this.parent = parent;
}
public void annoySister() {
parent.getSister().annoy();
}
}
public class Sister {
private final Parent;
public Sister(Parent parent) {
this.parent = parent;
}
public void annoy() {
System.out.println("I am annoyed");
}
public void parentsName() {
parent.getName();
}
}
public class Parent {
private final Brother brother;
private final Sister sister;
private final String name;
public Parent() {
this.name = "Johnny";
this.brother = new Brother(this);
this.sister = new Sister(this);
}
// getters
}
This way all the objects created in Parent's class are all available to each other. For example, in the code above, the Brother can access Sister methods and vice versa in addition to methods provided by Parent class (essentially accessing its state).
public class Main {
public static void main(String args) {
// somehow this seems to be like a container for all objects; ApplicationContext?
Parent parentInstance = new Parent();
parentInstance.getBrother().annoySister();
parentInstance.getSister().parentsName();
}
}
Q. How can this be passed to other classes, when Parent is being created as it'sits constructor is being executed?