有效的最终变量与最终变量 [英] Effectively final variable vs final variable

查看:82
本文介绍了有效的最终变量与最终变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中,Java设计人员提出了一个有效的最终变量的概念,即,如果要在变量"final"之后附加变量,则该变量不会产生编译器错误.我的问题是,相对于经典的决赛",新近提出的有效决赛"概念是什么?作为Java开发人员,我实际上能获得什么好处?

In Java 8, Java designers have come up with the notion of an effectively final variable, that is a variable which would not give compiler error if it were to be appended by 'final'. My question is what does this newly made concept of 'effectively final' offer over the classical 'final'? As a Java developer what benefits do I actually get?

推荐答案

最终的有效变量可以在lambda表达式中使用.这样想吧.这是一种情况-如果您想在内联/anonymous类中使用变量,则必须将其显式声明为final(请考虑使用Swing侦听器).现在,由于lambda表达式实际上是匿名内部类,因此您也可以有效地使用final变量.

The effectively final variables can be used in a lambda expression. Think about it this way. Here is a case - a variable had to be explicitly declared final if you wanted to use it in an inlined /anonymous class (think Swing listeners). Now you can also use effectively final variables, since lambda expressions are in effect anonymous inner classes.

这篇关于有效的最终变量与最终变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆