From Java8 Lambdas vs Anonymous classes:
"...[Anonymous Inner Classes] introduce a new scope. That is, names are resolved from the AIC's superclasses and interfaces and can shadow names that occur in the lexcially enclosing environment. For lambdas, all names are resolved lexically."
Given the above statement regarding scope, what would a Java anonymous inner class that executed differently were it simply converted to a Lambda look like?
These two pieces of code look like they would behave identically:
button.addClickHandler(clickEvent -> doSomething());
and
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent clickEvent) {
doSomething();
}
});
but would that be the case if the inner method was instead doSomething(this)
? or doSomething(myVariable)
?
An example of a situation where the change in scope matters and how it would behave would be SUPER AWESOME to see. I understand that there are other ways that Lambdas are different than anonymous inner classes, but it is this change in scope that I haven't found any good explanations of.
this
points to? That shouldn't be too hard. – shmoselthis
: My question could read as "Is the scope difference between an AIC and a Lambda such thatthis
is different between the two? If so, what does it point to in the case of a Lambda?" – Glen Pierce