java - Call constructor within constructor with self(this)-parameter -
i need link object of type b instance of type (circular dependencies). declare method, must called after constructor of , link new b a-instance. want achieve not having call such method manually. sample code:
public class a{ b b; public a(){ b = new b(this); // not work, // references object has not been created yet } } public class b{ a; public b(a a){ this.a = a; //or else } }
i commented problematic line. understand why can't work. need know is, if there well-known design pattern avoid problematic? or should redesign class model, putting in b a? suggestions?
it does work. it's problematic in exposes object before it's been initialized (so if b
constructor calls methods on parameter, example, bad thing), work. reference b.a
reference instance of a
that's been/being constructed.
i recommend trying avoid cyclic reference possible, alternative worse, code you've given work.
Comments
Post a Comment