![](/img/trans.png)
[英]Why local variable still showing warning though value is used in the method?
[英]Why does eclipse say Local variable is not used even though it is used?
我有以下代码
private List<String> getItems() {
XmlDoc document = new XmlDoc();
List<String> itemList = new ArrayList<String>();
String itemNum;
try {
XmlUtils root = document.parse(xmlFile);
List<XmlUtils> listNode = root.getChildNodes();
for (XmlUtils node : listNode) {
itemNum = node.getValue();
}
} catch (XmlException e) {
e.printStackTrace();
}
return itemList;
}
即使我在for循环eclipse中使用了String itemNum,也说“不使用局部变量itemNum的值”。 为什么会这样?
因为您要为itemNum
分配值,但从不读取它。 这是变量未使用的明确信号。 实际上,您可以安全地删除此行:
itemNum = node.getValue();
......什么都不会发生 也许你应该在分配之后使用itemNum
做一些事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.