我似乎找不到在我的静态方法中使用非静态引用的位置,代码是:
public class Item {
public static final Map ITEM_STATUSES = new HashMap();
static {
ITEM_STATUSES.put(STATUS_NEW, "New");
}
public static String getItemStatusFromName(final String p_itemStatusName) {
Iterator statusIterator = Item.ITEM_STATUSES.entrySet().iterator();
while (statusIterator.hasNext()) {
Entry statusEntry = (Entry)statusIterator.next();
if (((String)statusEntry.getValue()).equals(p_itemStatusName)) {
return (String)statusEntry.getKey();
}
}
return "";
}
}
在另一个类(class)
private void getName(){
String itemStatus = Item.getItemStatusFromName(p_itemStatusName);
}
编译器说:无法从类型 Item 对非静态方法 getItemStatusFromName(String) 进行静态引用
最佳答案
代码在我的电脑上编译得很好。如果在 Eclipse 或 Netbeans 等 IDE 中工作,请尝试清理项目并再次编译。
关于java - 我该如何解决 "Cannot make a static reference to the non-static field or method"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5581130/