java basis: When the local inner classes and anonymous inner class to access local variables, variables Why must add final?

Because inconsistent life cycle , local variables are stored directly on the stack, when the end of the method execution, non-final local variable is destroyed. The local inner class references to local variables still exist, if a local inner class to call the local variable, an error occurs. Plus the final, ensuring separate variables and local variables of the outer layer of the local inner class used to solve this problem.

Published 438 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_37769323/article/details/104603099