Java: reference escape

后端 未结 4 544
北荒
北荒 2020-12-07 11:38

Read that the following code is an example of \"unsafe construction\" as it allows this reference to escape. I couldn\'t quite get how \'this\' escapes. I am pretty new to t

4条回答
  •  醉梦人生
    2020-12-07 12:17

    I had the exact same doubt.

    The thing is that every class that gets instantiated inside other class has a reference to the enclosing class in the variable $this.

    This is what java calls a synthetic, it's not something you define to be there but something java does for you automatically.

    If you want to see this for yourself put a breakpoint in the doSomething(e) line and check what properties EventListener has.

提交回复
热议问题