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