简体   繁体   English

无法分配最终的局部变量标记,因为它是在封闭类型中定义的

[英]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 forEachlambda内的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.

相关问题 Java:最终的局部变量无法分配,因为它是用封闭类型定义的 - Java: the final local variable cannot be assigned, since it was defined in an enclosing type 最终局部变量checkstate不能分配,因为它是用封闭类型定义的 - The final local variable checkstate cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是用封闭类型定义的? - The final local variable cannot be assigned, since it is defined in an enclosing type? 无法分配最终的局部变量,因为它是在封闭类型中定义的 - The final local variable cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是在封闭类型java中定义的 - The final local variable cannot be assigned, since it is defined in an enclosing type java 最终的局部变量 dc 无法赋值,因为它是在封闭类型中定义的 - The final local variable dc cannot be assigned, since it is defined in an enclosing type 问题-无法分配最终的局部变量od,因为它是用封闭类型定义的 - Issue - The final local variable od cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是在封闭类型中定义的 - The final local variable cannot be assigned, since it is defined in an enclosed type 绕过“以封闭类型定义的最终局部变量” - Bypassing “final local variable defined in an enclosing type” 最终变量不能以封闭类型分配 - Final variables cannot be assigned in an enclosing type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM