0

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.

Community
  • 1
  • 1
Flame of udun
  • 2,136
  • 7
  • 35
  • 79
  • 1
    @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. – Flame of udun Oct 09 '16 at 22:09
  • @VineetKaushik In the linked question/answer, there's a link to an article that clearly explains what it means to let the this reference escape – fps Oct 09 '16 at 22:13

0 Answers0