简体   繁体   English

Java:最终的局部变量无法分配,因为它是用封闭类型定义的

[英]Java: the final local variable cannot be assigned, since it was defined in an enclosing type

How may one fix this problem? 如何解决这个问题?

The error lies @ scrollPane and map. 错误在于@scrollPane和map。

scrollPane error: the final local variable cannot be assigned, since it was defined in an enclosing type scrollPane错误:最终的局部变量无法分配,因为它是在封闭类型中定义的

map error suggestion: initialise variable. 映射错误建议:初始化变量。

final Map<ItemType, JScrollPane> viewScrollPanes = new HashMap<ItemType, JScrollPane>();
final JScrollPane scrollPane;
final Map<ItemType, JScrollPane> map;
this.viewList.forEach((type, list) -> {
  list.setSelectionMode(0);
  list.setVisibleRowCount(5);
  list.putClientProperty("type", type);
  list.setCellRenderer(this.itemRenderer);
  list.setName(String.valueOf(type.toString()) + "List");
  scrollPane = new JScrollPane(list);
  scrollPane.setMinimumSize(new Dimension(0, 110));
  scrollPane.setName(type.toString());
  map.put(type, scrollPane);
  return;
});

You cannot assign to a final variable repeatedly in a loop. 您不能在循环中重复分配给final变量。

It looks like scrollPane should be a local variable inside of each iteration. 看起来scrollPane应该是每次迭代内部的局部变量。

map needs be initialized before you enter the loop (otherwise you cannot put anything in it). 进入循环之前, map需要初始化(否则,您不能在其中put任何内容)。

You can't. 你不能 You define a final variable, which you can store only one and final object. 您定义一个最终变量,该变量只能存储一个和最终对象。 You did a for loop, and for each iteration, you try to assign a new object in scrollpane, but the variable is marked as a final object. 您进行了一个for循环,并且对于每次迭代,您尝试在滚动窗格中分配一个新对象,但是该变量被标记为最终对象。

So the only way is to remove the final keyword. 因此,唯一的方法是删除最终关键字。

About map, remove the final keyword and put this instead 关于地图,删除最终关键字,然后改用该关键字

Map<ItemType, JScrollPane> map = new HashMap<>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最终局部变量无法分配,因为它是在封闭类型java中定义的 - The final local variable cannot be assigned, since it is defined in an enclosing type java 最终局部变量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 无法分配最终的局部变量标记,因为它是在封闭类型中定义的 - The final local variable token cannot be assigned, since it is defined in an enclosing type 最终的局部变量 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