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

前端 未结 20 2214
一向
一向 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:29

    Because it's confusing if the variable isn't final, as the changes to it won't be picked up in the anonymous class.

    Just make the variables 'price' and 'lastPrice' final.

    -- Edit

    Oops, and you'll also need to not assign to them, obviously, in your function. You'll need new local variables. Anyway, I suspect someone has given you a better answer by now.

提交回复
热议问题