java Error---Lambda expression's local variable e cannot re-declare another local variable defined e

在使用lambda表达式时,为控件添加事件响应函数时,出现:

Lambda expression's local variable e cannot re-declare another local variable defined e

出错的代码段如下:

stage.setOnCloseRequest(e->{
						...
						...
					});

从错误提示可以看出是对e进行了重复的声明,检查代码,发现果然在代码中有这样一段:

btn.setOnAction(e->{
					stage.show();
				});

所以,很简单,只要把e换成别的名字就好了!但是在查解决方案时在Oracle官网发现了更为权威的的解释:

Local Variables in Lambda Expressions
A lambda expression does not define a new scope; the lambda expression scope is the same as the enclosing scope. For example, if a lambda expression body declares a local variable with the same name as a variable in the enclosing scope, a compiler error—Lambda expression’s local variable i cannot re-declare another local variable defined in an enclosing scope—gets generated, as shown in Figure 3.
oracle

官网的解释:lambda表达式不定义新范围, lambda表达式作用域与封闭作用域相同。例如,如果lambda表达式主体声明一个与封闭范围中的变量同名的局部变量,这个时候就需要使用其他的变量名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值