How can I solve "Unable to make a static link to non-static fields or method"?

I can not find where I use non-static links in my static method, the 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 another class

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

The compiler says: It is not possible to make a static reference to the non-static method getItemStatusFromName (String) of type Item

+4
source share
5 answers

The code compiles on my computer. If you are working in an IDE such as Eclipse or Netbeans, try cleaning up the project and compiling again.

+2
source

First of all, consider using a different type of storage.

Here is an example that I whipped 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")); } } 

The changes I would make are to change the map to use an enumeration instead of a string, and return the enumeration instead of a string. But it is up to you and your coding requirements.

This code definitely works for me.

0
source

Your code looks great (except that it doesn't use 1.5 functions like generics and extended for-loops). If I insert and run it, it works. So for me it looks like a problem with classpath: there may be a version of Item that does not have a static getItemStatusFromName, and your compiler is trying to use this version instead of your version. If you packed the item in a JAR and re-pointed the JAR elsewhere, first update the corresponding JAR.

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

0
source

First, you should consider why everything in your class is static. You should consider static == Class (non-synthetic variables). However, your code calls HashMap. Based on the class name, you must remove the static keyword, add STATUS_NEW in the constructor to ITEM_STATUSES, and continue development.

By the way, if you are using Java 5.0 or higher, try the following:

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

Good luck

0
source

Are you sure that there is only one Item class in the classpath? Finding the Item type in the IDE is probably some wrong configuration. What IDE are you using? If an Idea, always try File: Invalidate Caches ... - they are often mistaken.

0
source

Source: https://habr.com/ru/post/1347098/


All Articles