Cannot refer to a non-final variable inside an inner class defined in a different method

前端 未结 20 2745
一向
一向 2020-11-21 05:04

Edited: I need to change the values of several variables as they run several times thorugh a timer. I need to keep updating the values with every iteration through the timer

20条回答
  •  不要未来只要你来
    2020-11-21 05:44

    You cannot refer to non-final variables because Java Language Specification says so. From 8.1.3:
    "Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final." Whole paragraph.
    I can see only part of your code - according to me scheduling modification of local variables is a strange idea. Local variables cease to exist when you leave the function. Maybe static fields of a class would be better?

提交回复
热议问题