Reputation: 1007
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
Upvotes: 4
Views: 6884
Reputation: 20594
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.
Upvotes: 0
Reputation: 386
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!
Upvotes: 0
Reputation: 8932
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.
Upvotes: 0
Reputation: 4951
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.
Upvotes: 0
Reputation: 5023
The code compiles fine on my computer. If working in an IDE like Eclipse or Netbeans, try cleaning the project and compiling again.
Upvotes: 2