java - composite inheritance: how to assign a final field at sub-class constructor which depends on 'this' value (backward reference)? -


i use composite classes group functionalities.
but, class (with composite a1), got inherited b (with composite b1), , behavior existent @ a1 going adapted @ b1, final a1 must b1 instance work.

obs.: have ways make sure composite instantiation happens (only composite partner).

unable assign b1 object a1 final field:

class finalfieldtestfails{     class a1{       a1(a a){}     }      class a{       protected final a1 a1;       a(){         this.a1 = new a1(this);       }        a(a1 a1){         this.a1 = a1;       }     }      class b1 extends a1{       b1(b b){         super(b);       }     }      class b extends a{         //b(){ super.a1=new b1(this); } //fail: cant change final value         //b(){super(new b1(this));} //fail: cant use 'this' or 'super'     } } 

ps.: answer shall not involve reflection security tricks if possible.

b(){ super.a1=new b1(this); } //fail: cant change final value

you can not assign values assigned final variables, can instantiate within constructor not following.

b(){super(new b1(this));} //fail: cant use 'this' or 'super'

you can use once instance created only. since there inheritance tree, not instantiated, compiler complains,

cannot reference before supertype constructor has been called


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -