Results 1 to 8 of 8
Thread: Enums taking in enums?
- 06-10-2011, 10:10 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
Enums taking in enums?
Is there a way to make an enum have an enum as one of its constructors parameters?
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 saysJava 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()); } }
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."Cannot refer to the static enum field Type.color within an initializer"
-
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; } }
- 06-10-2011, 10:20 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
Is .FOO whatever color constant/enum I have in Color? like Color.WHITE?
-
- 06-10-2011, 11:17 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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.
- 06-11-2011, 08:45 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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:
I don't want to necessarily hard code the color mainly because it could either be black, blue, or green.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 hope this sheds some light on my position :D
- 06-11-2011, 01:40 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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: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.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; } }
Similar Threads
-
taking something from an array.
By shazakala in forum New To JavaReplies: 1Last Post: 05-10-2011, 01:36 AM -
trying to learn enums and arrays
By Gerrburge in forum New To JavaReplies: 9Last Post: 02-02-2011, 02:54 PM -
Returning flags from enums
By willemien in forum New To JavaReplies: 5Last Post: 05-26-2010, 07:37 AM -
declaring problems with enums
By jackrulesok in forum New To JavaReplies: 10Last Post: 04-30-2010, 10:16 AM -
why we are using enums in Java?
By manish.anchan in forum New To JavaReplies: 7Last Post: 01-08-2010, 04:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks