A properly constructed object is one that doesn't let the this reference escape
.
I don't understand the above definition. I looked up a few posts on SO like this one and also this but I don't understand what it means for the this
reference to escape
. Here is the example mentioned in the link :
public class ThisEscape {
public ThisEscape(EventSource source) {
source.registerListener(
new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
});
}
}
Can someone in very simple words and plain manner explain how the this
reference escapes here?
EDIT
@FedericoPeraltaSchaffner the question asks something entirely different. The questioner has asked, highlighted in bold by the questioner himself, how to protect against this
reference escaping. I am asking what does it MEAN when it is said `this reference has escaped.