Results 1 to 8 of 8
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Enums taking in enums?

    Is there a way to make an enum have an enum as one of its constructors parameters?
    Java Code:
    package piece;
    
    import game.operation.Board;
    import game.operation.Location;
    
    public enum Type {
    	
    
    	CHEESE(color){
    
                   //methods go here
    
    	};
    
    	public static Color color;
    
    	private Type( Color color) {
    		this.[COLOR="red"]color[/COLOR] = Color.valueOf(Color.class, color.toString());
    	}
    }
    I have tried to use the static method .valueOf(); which works. But the problem is that I get an error when setting it to static. It says
    "Cannot refer to the static enum field Type.color within an initializer"
    Color is it's own enum. I want to define that my cheese is color Blue, black, green or what ever I have put in Color enum.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For one, don't make Color static if each enum member is to have its own color. And don't use valueOf. Just accept the color passed in:

    Java Code:
    public enum Type {
    	
    
    	CHEESE(Color.FOO){ // show which Color here
    
                   //methods go here
    
    	};
    
    	private Color color;
    
    	private Type( Color color) {
    		this.color = color;
    	}
    
    	public  getColor( ) {
    		return color;
    	}
    }

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Is .FOO whatever color constant/enum I have in Color? like Color.WHITE?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rizowski View Post
    Is .FOO whatever color constant/enum I have in Color? like Color.WHITE?
    Yes. If that doesn't work for you, then please elaborate more on the nature of your problem.

  5. #5
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    What I am trying to make it abstract so that it doesn't need to be a specific color. it "can" be blue or yellow.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rizowski View Post
    What I am trying to make it abstract so that it doesn't need to be a specific color. it "can" be blue or yellow.
    Sorry but I don't understand what you mean here. If you want to change the state of a Java object, you usually do so by setter methods. Abstract has nothing to do with this, nor can you have abstract enums. I think that if you still need help, you'll have to give us more detail. Assume that we can't read your mind or guess what your un-shown code looks like.

  7. #7
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    It is probably not really Abstraction I believe the word i was actually looking for is polymorphism. I don't really want to set the specific color of cheese. Because it could be black, blue, or green. I am really unsure how I would be able to make it generic so that if I called the enum "Type" I could say something like Type.Cheese.color.toString(); and it would print out the color of the cheese. The way I have my Color enum is Like so:
    Java Code:
    public enum Color {
    
    	WHITE("White", "L"),
    	BLACK("Black", "D");
    	
    	public final String COLOR;
    	public final String SHORT_COLOR;
    	private Color(String color, String shortColor) {
    		this.COLOR = color;
    		this.SHORT_COLOR = shortColor;
    	}
    }
    I don't want to necessarily hard code the color mainly because it could either be black, blue, or green.

    I hope this sheds some light on my position :D

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You seem to be confused about what the enum value CHEESE is in your Type enum. CHEESE is a specific, once-only instantiation of Type that must be given an explicit Color. The Type constructor is what is called by, for example, the CHEESE(Color.blue) statement in the Type body. Enum values are instantiated once (with specific parameters if required), they aren't dynamic.

    If you want cheeses of different kinds by color, you should define a Cheese enum with the different colors:
    Java Code:
    public enum Cheese {
        CHEDDAR(Color.white) {
           
        },
        STILTON(Color.blue){
               //methods go here
        };
        ...  // etc.
        
        public Color color;
    
        private Cheese(Color color) {
    	this.color = color;
        }
    }
    If you really want to be able to instantiate a cheese object with a variable Color parameter, you need to write a Cheese class, not an enum.

Similar Threads

  1. taking something from an array.
    By shazakala in forum New To Java
    Replies: 1
    Last Post: 05-10-2011, 01:36 AM
  2. trying to learn enums and arrays
    By Gerrburge in forum New To Java
    Replies: 9
    Last Post: 02-02-2011, 02:54 PM
  3. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 07:37 AM
  4. declaring problems with enums
    By jackrulesok in forum New To Java
    Replies: 10
    Last Post: 04-30-2010, 10:16 AM
  5. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 04:41 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
  •