Inner class and local variables

后端 未结 6 562
无人共我
无人共我 2020-11-30 12:48

Why do I need to declare a local variable as final if my Inner class defined within the method needs to use it ?

Example :

6条回答
  •  臣服心动
    2020-11-30 13:14

    It's because the inner class inside a function actually makes a copy of the local variable because local variable may be destroyed after the function call while the instance of the local class still exists. To make the two copies of the local variable always have the same value(to make them seem identical), you have to declare the variable as final.

提交回复
热议问题