[英]The final local variable token cannot be assigned, since it is defined in an enclosing type
I'm getting The final local variable token cannot be assigned, since it is defined in an enclosing type
compilation error at the token
inside the forEach
and lambda
.我得到The final local variable token cannot be assigned, since it is defined in an enclosing type
forEach
和lambda
内的token
处的The final local variable token cannot be assigned, since it is defined in an enclosing type
编译错误中The final local variable token cannot be assigned, since it is defined in an enclosing type
。 All I want to do is assign the only item in the list to token
variable.我想要做的就是将列表中唯一的项目分配给token
变量。
PS: I know for sure that there is only one item in the list
. PS:我确定list
只有一项。
public static void main(String[] args) throws Exception {
final StringBuilder token;
AuthResponse authResponse = new AuthResponse();
authResponse.getTokens().forEach(item -> {
token.append(item.getToken());
});
}
If I declare token
as an Instance variable
there is no problem.如果我将token
声明为Instance variable
,则没有问题。 But is there any other way to do it?但是有没有其他方法可以做到这一点?
There are a few posts regarding this but I'd like to understand the resolution for this issue有一些关于此的帖子,但我想了解此问题的解决方案
EDIT : I have updated the question to have StringBuilder
instead of String
编辑:我已更新问题以使用StringBuilder
而不是String
You need to assign an instance:您需要分配一个实例:
StringBuilder token = new StringBuilder();
But why are you reinventing the wheel;但是你为什么要重新发明轮子? use something like:使用类似的东西:
String tokens = new AuthResponse().getTokens().stream()
.map(Object::toString)
.collect(Collectors.joining(","));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.