java - 我该如何解决 "Cannot make a static reference to the non-static field or method"?

标签 java static-methods

我似乎找不到在我的静态方法中使用非静态引用的位置,代码是:

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/

相关文章:

java - Java 的浏览器兼容性问题

java - JSON : serialization from python to Java

java - 如何在大型Java项目中查找静态方法调用?

python - 在 python 中使用静态方法 - 最佳实践

c# - 返回静态方法所在的类名

java - Google Container Engine 中集群中 pod 的组织

java - println(String s) 与 println(Object o)

java - 如何为列表中的类的所有实例调用方法?

python - 如何推断 @staticmethod 所属的类?

带有静态数组的java空指针异常