I think the easiest way to describe my question is with an example:
class Enclosing {
private ObjectOfInterest o = new ObjectOfInterest();
public void registerEventListener() {
EventEmitter.onEvent(() -> {
// Need to access 'o' here, while 'this' is not important.
o.yeeha();
});
}
}
Does the lambda expression hold an implicit reference to its Enclosing
instance? If so, how can I avoid this to avoid memory leaks? I need to access o
in the event listener, but not the enclosing instance.
BTW, I found the question "Does Java8 lambdas maintains a reference to their enclosing instance like anonymous classes?", but it is not clear to me when exactly the lambda expression is considered to "not capture members from the enclosing instance". In addition, the document that is referenced in the accepted answer declares itself as outdated.