如何解决“无法对非静态字段或方法进行静态引用”?

发布于 2024-10-31 03:37:34 字数 768 浏览 4 评论 0原文

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

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 "";
 }
}

并且在另一个类中

private void getName(){
  String itemStatus = Item.getItemStatusFromName(p_itemStatusName);
}

编译器说:无法对非静态方法 getItemStatusFromName(String) 进行静态引用类型 项目

I can't seem to find where I'm using a non-static reference in my static method, code is:

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 "";
 }
}

and in the other class

private void getName(){
  String itemStatus = Item.getItemStatusFromName(p_itemStatusName);
}

Compiler says: Cannot make a static reference to the non-static method getItemStatusFromName(String) from the type Item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

格子衫的從容 2024-11-07 03:37:38

您确定类路径中只有一个 Item 类吗?使用 IDE 搜索类型 Item - 可能存在一些配置错误。您使用哪个 IDE?如果有想法,请务必尝试文件:使缓存无效... - 它们通常是错误的。

Are you sure, you have only one Item class in classpath? Search for type Item in with the IDE - maybe some misconfiguration. Which IDE do you use? If Idea, always try File : Invalidate Caches ... - they are often wrong.

淡紫姑娘! 2024-11-07 03:37:37

该代码在我的计算机上编译良好。如果在 Eclipse 或 Netbeans 等 IDE 中工作,请尝试清理项目并再次编译。

The code compiles fine on my computer. If working in an IDE like Eclipse or Netbeans, try cleaning the project and compiling again.

隔纱相望 2024-11-07 03:37:37

首先,考虑使用不同类型的商店。

这是我编写的一个示例,它实际上执行了相同的操作,并且肯定可以编译并运行:

import java.util.HashMap;
import java.util.Map;

class ItemMap {
static Map<String, String> statuses = new HashMap<String, String>();

static {
    statuses.put("STATUS_NEW", "New");
}

public static String getStatusFromString(String s) {
    for (Map.Entry<String, String> e : statuses.entrySet()) {
        if (e.getValue().equals(s)) {
            return e.getKey();
        }
    }
    return "";
}
}

public class Item {
public static void main(String[] args) {
    System.out.printf("Status for 'New': %s%n",
            ItemMap.getStatusFromString("New"));
}
}

我所做的更改是修改映射以使用枚举而不是字符串,并返回枚举而不是字符串。但这取决于您和您的编码要求。

这段代码绝对适合我。

First off, consider using a different type of store.

Here's an example I whipped up that does something effectively the same, and definitely compiles and runs:

import java.util.HashMap;
import java.util.Map;

class ItemMap {
static Map<String, String> statuses = new HashMap<String, String>();

static {
    statuses.put("STATUS_NEW", "New");
}

public static String getStatusFromString(String s) {
    for (Map.Entry<String, String> e : statuses.entrySet()) {
        if (e.getValue().equals(s)) {
            return e.getKey();
        }
    }
    return "";
}
}

public class Item {
public static void main(String[] args) {
    System.out.printf("Status for 'New': %s%n",
            ItemMap.getStatusFromString("New"));
}
}

Changes I'd put in would be to modify the map to use an enum instead of a String, and return the enum instead of the String. But that's up to you and your coding requirements.

This code definitely works for me.

娇纵 2024-11-07 03:37:37

您的代码看起来非常好(除了不使用 1.5 功能,例如泛型和扩展 for 循环)。如果我粘贴并运行它,它就会起作用。所以对我来说,它看起来像一个类路径问题:可能有一个 Item 版本没有静态 getItemStatusFromName,并且您的编译器尝试使用此版本而不是您的版本。如果您已将 Item 打包到 JAR 中并在其他地方引用该 JAR,则首先更新相应的 JAR。

如果您不确定,那么最好的猜测是通过 ClassLoader c = Item.class.getClassLoader(); 访问 Item 的类加载器,然后使用调试器找出类加载器在哪里从中获取其文件。

Your code looks perfectly fine (apart from not using 1.5 features such as generics and extended for-loops). If I paste and run it, it works. So for me, it looks like a classpath issue: There may be a version of Item that doesn't have a static getItemStatusFromName, and your compiler tries to use this version instead of your version. If you have packaged Item into a JAR and refrence the JAR somewhere else, then update the respective JAR first.

If youy are not sure, then the best guess would be to access the class loader of Item via ClassLoader c = Item.class.getClassLoader(); and then use the debugger to find out where the class loader fetches its files from.

无风消散 2024-11-07 03:37:37

首先,您应该考虑为什么类中的所有内容都是静态的。您应该考虑 static == Class (非实例变量)。但是,您的代码实例化了 HashMap。根据类的名称,您应该删除 static 关键字,将构造函数中的 STATUS_NEW 添加到 ITEM_STATUSES 中并继续开发。

顺便说一句,如果您使用的是 Java 5.0 或更高版本,请尝试以下操作:

Map<Object, String> map = new HashMap<Object, String>();
for (String str : map.values()) {
    srt.doSomething();
}

祝您好运!

First, you should think about why everything in your class is static. You should consider static == Class (non instanced variables). However, your code instances a HashMap. Based in the name of the class, you should remove the static keyword, add the STATUS_NEW in the constructor into the ITEM_STATUSES and continue developing.

BTW, if you are using Java 5.0 or superior, try this:

Map<Object, String> map = new HashMap<Object, String>();
for (String str : map.values()) {
    srt.doSomething();
}

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文