Results 1 to 2 of 2
  1. #1
    Aggnavarius is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Referring to enum values with instantiation of an object

    I've got the following enum class:


    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
    }
    In another class, I want to pass a tileName value to a function like this:

    Java 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:


    Java 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):

    Java Code:
    import static tileName.*;
    
    foo(GROUND_NOTHING);
    Last edited by Aggnavarius; 09-18-2012 at 07:42 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Referring to enum values with instantiation of an object

    The usual way would be:
    Java Code:
    foo(tileName.GROUND_NOTHING);
    Though that should be 'TileName' if you were using the proper Java naming conventions.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 10:45 AM
  2. referring a unknown object in its subclass
    By Sliks in forum Advanced Java
    Replies: 3
    Last Post: 06-18-2011, 12:20 PM
  3. Referring to a super class
    By blug in forum New To Java
    Replies: 7
    Last Post: 03-20-2011, 05:06 AM
  4. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  5. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •