inner class non-final variable java

前端 未结 4 1541
滥情空心
滥情空心 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:13

    Since you appear to be setting multiple things (from the comments), make a method in the main class, button1WasClicked(), (a better name might be doUpdate, doSave etc. - something relevant to what the button does), put the proper code there, and call it from the inner class / listener. (If you are using Swing I'd make it an Action, YMMV)

    That way if later on there is a menu or an intent or a gesture that needs to execute the same stuff, the call is there.

提交回复
热议问题