How does marking a variable as final allow inner classes to access them?

后端 未结 6 1090
天涯浪人
天涯浪人 2020-12-06 00:49

An inner class defined inside a method cannot access the method\'s local variables unless these local variables are marked final.I\'ve looked a

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 01:13

    Method Local inner classes can’t use local variable of outer method until that local variable is not declared as final. The main reason we need to declare a local variable as a final is that local variable lives on stack till method is on the stack but there might be a case the object of inner class still lives on the heap.

    checkout this article: http://www.geeksforgeeks.org/inner-class-java/

提交回复
热议问题