inner class non-final variable java

前端 未结 4 1536
滥情空心
滥情空心 2021-02-15 14:59

I needed to change variables inside an inner class and I got the infamous \"Cannot refer to a non-final variable inside an inner class defined in a different method\" error.

4条回答
  •  失恋的感觉
    2021-02-15 15:19

    I would keep a reference to your on click listener in the outer class, and make the int a member variable in your listener. Just store the variable in the listener on click, then grab the variable in the outer class when you need it, rather than setting it at the point of the click.

    To put it simply, if the inner class needs to change it, make it a variable in the inner class.

提交回复
热议问题