<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi Ella,<br>
the problem here is that, since the lambda needs to refer to
"this", it's as if you need to pass "this" to create the lambda
object.</p>
<p>To make it simpler, you can think of the lambda as a local class
(not entirely accurate, but I think it paints a good analogy for
what's going on here):</p>
<p>class Main {<br>
int a;<br>
Main() {<br>
this.a = 1; //line A<br>
class Foo {<br>
Main this$0;<br>
Foo(Main this$0) { this.this$0 = this$0; }<br>
void run() { this$0.a = 1; }<br>
}<br>
Foo lmb = new Foo(this); // line A<br>
<br>
lmb.run(); //line B<br>
super();<br>
}<br>
</p>
<p><br>
</p>
<p>Like before, in line (A) we create the lambda expression (here
modelled as a local class). Note that the local class wants a
construction parameter, of type Main (since the local class needs
to refer to it). But then, in line (B), we need to supply an
argument of type Main, namely "this". So here we are effectively
reading/accessing "this" before the super constructor has been
called.</p>
<p>The lambda expression, being so compact, hides a bit of the
problem and you are right that it's a bit confusing at first, but
I hope the example above clarifies things a bit.</p>
<p>Cheers<br>
Maurizio<br>
</p>
<div class="moz-cite-prefix">On 05/06/2024 22:29, Ella Ananeva
wrote:<br>
</div>
<blockquote type="cite" cite="mid:DS7PR10MB59995175EC821EC677948BC1FBF92@DS7PR10MB5999.namprd10.prod.outlook.com">
<p class="MsoNormal"><span style="font-size:11.0pt">Thanks for
pointing this out, Chen.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Please bear
with me for a moment. If we have a lambda as a local variable
in the constructor, it cannot be invoked outside of the
constructor, right?
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">class
</span><span style="font-size:10.0pt;font-family:"Courier New";color:black">Main
</span>
<span style="font-size:10.0pt;font-family:"Courier New";color:#080808">{<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">int
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#871094">a</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#00627A">Main</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">()
{<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">this</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.a
=
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;
//</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C">line
A<br>
</span></i><span style="font-size:10.0pt;font-family:"Courier New";color:black">Foo
lmb
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">=
() ->
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">this</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.a
=
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">;</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C">
<o:p></o:p></span></i></p>
<p class="MsoNormal" style="background:white"><i><span style="font-size:10.0pt;font-family:"Courier New";color:#8C8C8C"> </span></i><span style="font-size:10.0pt;font-family:"Courier New";color:black">lmb</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.foo();
//line B<br>
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">super</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">();<br>
}</span></p>
</blockquote>
</body>
</html>