Local class can access non-final variable in java 8

后端 未结 1 1897
面向向阳花
面向向阳花 2020-12-11 01:20

Before Java 8, We were not able to use non-final variables inside local class. But now they are allowing final as well as effectively final(who\'s values has not been change

相关标签:
1条回答
  • 2020-12-11 01:33

    The situation has not changed at all, actually. The compiler is just a bit smarter, and doesn't force you to use the final keyword anymore.

    If it detects that the variable is effectively final, i.e. assigned only once, and never after, everything is good. If it detects that it's not effectively final, it refuses to compile.

    So, instead of forcing you to make a variable final, it detects it automatically. But you still can't use non-effectively-final variables inside an inner class or lambda.

    0 讨论(0)
提交回复
热议问题