Referring to enum values with instantiation of an object
I've got the following enum class:
Code:
package org.Temporis.gameClasses;
public enum tileName {
GROUND_NOTHING,
MID_NOTHING,
TOP_NOTHING,
GROUND_DIRT01,
GROUND_DIRT02,
GROUND_GRASS01,
GROUND_WATER01,
MID_WALL01,
MID_BUSH01,
TOP_TREE01
}
In another class, I want to pass a tileName value to a function like this:
Code:
foo(GROUND_NOTHING);
If I do that, I get "GROUND_NOTHING cannot be resolved to a variable." error in Eclipse. I have to do this instead:
Code:
tileName a;
foo(a.GROUND_NOTHING);
Is this the way enums are supposed to be used? I get the feeling that I'm doing something wrong, since it seems like you should just be able to refer to the values without creating an object.
EDIT: Wow, it's amazing how often you figure something out RIGHT after you ask. Anyway, for those who read this post and may want to know the answer, it turns out you can do a static import.
The following actually works (assuming you put in the correct path for tileName):
Code:
import static tileName.*;
foo(GROUND_NOTHING);
Re: Referring to enum values with instantiation of an object
The usual way would be:
Code:
foo(tileName.GROUND_NOTHING);
Though that should be 'TileName' if you were using the proper Java naming conventions.