Setting outer variable from anonymous inner class

后端 未结 9 843
臣服心动
臣服心动 2020-11-28 07:03

Is there any way to access caller-scoped variables from an anonymous inner class in Java?

Here\'s the sample code to understand what I need:

public L         


        
9条回答
  •  迷失自我
    2020-11-28 08:10

    If the containing class is MyClass -->

    MyClass.this.variable = value;
    

    Do not remember if this would work with a private variable (I think it would work).

    Only works for attributes of the class (class variable). Does not work for method local variables. In JSE 7 probably there will be closures to do that kind of thing.

提交回复
热议问题