i'm initializing object, , in constructor, passing lambda. in lambda, access object initializing.
myobject obj = new myobject(params -> { this.xyz(); //tries access scope of class surrounding obj myobject.this.xyz(); //error: 'myobject' not surrounding class obj.xyz(); //error: variable 'obj' might not have been initialized. });
example myobject
class:
public class myobject { //missing constructor public void xyz(){ //do stuff } }
i rather not pass this
lambda if possible.
if there way "temporary final variable", can't find it.
you cannot this, because compiler must assume constructor going make full use of lambda, i.e. possibly call it.
in case call xyz()
must routed object has not been initialized yet; java compiler cannot allow that.
the workaround convince java not going use partially initialized object during construction. can constructing object first, , setting lambda later:
myobject obj = new myobject(); obj.setlambda(params -> { obj.xyz(); });
one unfortunate consequence of work-around lambda variable cannot marked final
inside myobject
class.
Comments
Post a Comment