Reputation: 1187
If have this enum type code and get this compile error.
I get that Color.RED
is an int but Im not sure why I am getting the error?
enum Direction {
NORTH(Color.RED), WEST(Color.BLUE), EAST(Color.BLACK), SOUTH(
Color.GREEN);
Direction(Color c) {
color = c;
}
private Color color;
public static Direction fromName(String name) {
for (Direction d : Direction.values()) {
if (d.name().equalsIgnoreCase(name)) {
return d;
}
}
return NORTH;
}
public Color getColor() {
return color;
} }
Thanks for the help in advance.
Upvotes: 0
Views: 137
Reputation: 449
public enum Direction {
NORTH(Color.RED), WEST(Color.BLUE), EAST(Color.BLACK), SOUTH(
Color.GREEN);
Direction(int c) {
color = c;
}
private int color;
public int get() {
return color;
}
}
and to get color in int format use :
Direction.SOUTH.get();
which will return int value for color "Green"
Upvotes: 3
Reputation: 1029
In your constructor you declare that the parameter has to be an instance of the class Color
. Color.RED
for instance however is returning an Integer.
So you would either
Color.RED
and so on with objects of Color
OR
int
.Upvotes: 2