Are all final variables captured by anonymous classes?

后端 未结 4 1237
北恋
北恋 2021-02-13 00:03

I thought I knew the answer to this, but I can\'t find any confirmation after an hour or so of searching.

In this code:

public class Outer {

    // othe         


        
4条回答
  •  一生所求
    2021-02-13 00:22

    obj2 will be garbage collected since it has no reference to it. obj1 will not be garbage collected as long as the event is active since even if you created an anonymous class, you've created a direct reference to obj1.

    The only thing final does is that you can't re-define the value, it doesn't protect the object from the garbage collector

提交回复
热议问题