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

前端 未结 20 2213
一向
一向 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条回答
  •  -上瘾入骨i
    2020-11-21 05:29

    To solve the problem above, different languages make different decisions.

    for Java, the solution is as what we see in this article.

    for C#, the solution is allow side-effects and capture by reference is the only option.

    for C++11, the solution is to allow the programmer make the decision. They can choose to capture by value or by reference. If capturing by value, no side-effects would occur because the variable referenced is actually different. If capture by reference, side-effects may occur but the programmer should realize it.

提交回复
热议问题