Results 1 to 2 of 2
- 09-18-2012, 07:36 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Referring to enum values with instantiation of an object
I've got the following enum class:
In another class, I want to pass a tileName value to a function like this:Java 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 }
If I do that, I get "GROUND_NOTHING cannot be resolved to a variable." error in Eclipse. I have to do this instead:Java Code:foo(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.Java Code:tileName a; foo(a.GROUND_NOTHING);
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):
Java Code:import static tileName.*; foo(GROUND_NOTHING);
Last edited by Aggnavarius; 09-18-2012 at 07:42 AM.
- 09-18-2012, 09:49 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
public static enum vs enum class
By Dipke in forum New To JavaReplies: 3Last Post: 08-30-2011, 10:45 AM -
referring a unknown object in its subclass
By Sliks in forum Advanced JavaReplies: 3Last Post: 06-18-2011, 12:20 PM -
Referring to a super class
By blug in forum New To JavaReplies: 7Last Post: 03-20-2011, 05:06 AM -
Setting values from One Enum type to another enum type.
By reach2sudhakar in forum New To JavaReplies: 3Last Post: 09-23-2010, 06:02 PM -
object instantiation and arrayList
By lockmac in forum New To JavaReplies: 5Last Post: 08-09-2007, 06:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks