Somehow you need to pass the instance of B
to C
during construction. The simplest way is to have a constructor on C
:
public class C {
private B myInstanceOfB;
public C(B instance) {
this.myInstanceOfB = instance;
}
}
Now this would mean that whoever creates a C
instance must know to do that and have access to a B
instance. It's possible that you want to "hide" that requirement, then you can do things like add a factory method for C
into the A
class:
public class A {
private B instanceOfB;
public C createC() {
return new C(instanceOfB);
}
}
If you do this you can also make the C
constructor package-private to indicate to potential users that they should not attempt to instantiate it directly (and document in the JavaDoc how to get a C
instance).
Whether or not that makes sense depends on what the relation between A and C is.