![](/img/trans.png)
[英]The final local variable cannot be assigned, since it is defined in an enclosing type java
[英]Java: the final local variable cannot be assigned, since it was defined in an enclosing type
如何解决这个问题?
错误在于@scrollPane和map。
scrollPane错误:最终的局部变量无法分配,因为它是在封闭类型中定义的
映射错误建议:初始化变量。
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;
});
您不能在循环中重复分配给final
变量。
看起来scrollPane
应该是每次迭代内部的局部变量。
进入循环之前, map
需要初始化(否则,您不能在其中put
任何内容)。
你不能 您定义一个最终变量,该变量只能存储一个和最终对象。 您进行了一个for循环,并且对于每次迭代,您尝试在滚动窗格中分配一个新对象,但是该变量被标记为最终对象。
因此,唯一的方法是删除最终关键字。
关于地图,删除最终关键字,然后改用该关键字
Map<ItemType, JScrollPane> map = new HashMap<>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.