Urgh, I'm kind of confused on how enums work in Java. In C# and C++ (what I use normally), this seems okay, but Java wants to get mad at me >.>
enum Direction
{
NORTH_WEST = 0x0C,
NORTH = 0x10,
NORTH_EAST = 0x14,
WEST = 0x18,
NONE = 0x20,
EAST = 0x28,
SOUTH_WEST = 0x24,
SOUTH = 0x30,
SOUTH_EAST = 0x3C
}
Could someone tell me what I'm doing wrong? Thanks
Here are the errors:
----jGRASP exec: javac -g Test.java
Test.java:79: ',', '}', or ';' expected
NORTH_WEST = 0x0C,
^
Test.java:79: '}' expected
NORTH_WEST = 0x0C,
^
Test.java:80: <identifier> expected
NORTH = 0x10,
^
Test.java:87: ';' expected
SOUTH_EAST = 0x3C
^
An enum is a special class that represents a group of constants. To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. values() method can be used to return all values present inside enum.
No, we can have only strings as elements in an enumeration. Using strings in it, generates a compile time error.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the Convert class.
For this scenario, it looks like you can simply use an instance field.
public enum Direction {
NORTH(0x10), WEST(0x18), ...;
private final int code;
Direction(int code) { this.code = code; }
public int getCode() { return code; }
}
Java enum
are implemented as objects. They can have fields and methods. You also have the option of declaring a constructor that takes some arguments, and providing values for those arguments in your constant declaration. You can use these values to initialize any declared fields.
enum
EnumSet
and EnumMap
Note that depending on what these values are, you may have an even better option than instance fields. That is, if you're trying to set up values for bit fields, you should just use an EnumSet
instead.
It is common to see powers of two constants in, say, C++, to be used in conjunction with bitwise operations as a compact representation of a set.
// "before" implementation, with bitwise operations
public static final int BUTTON_A = 0x01;
public static final int BUTTON_B = 0x02;
public static final int BUTTON_X = 0x04;
public static final int BUTTON_Y = 0x08;
int buttonState = BUTTON_A | BUTTON_X; // A & X are pressed!
if ((buttonState & BUTTON_B) != 0) ... // B is pressed...
With enum
and EnumSet
, this can look something like this:
// "after" implementation, with enum and EnumSet
enum Button { A, B, X, Y; }
Set<Button> buttonState = EnumSet.of(Button.A, Button.X); // A & X are pressed!
if (buttonState.contains(Button.B)) ... // B is pressed...
There is also EnumMap
that you may want to use. It's a Map
whose keys are enum
constants.
So, where as before you may have something like this:
// "before", with int constants and array indexing
public static final int JANUARY = 0; ...
Employee[] employeeOfTheMonth = ...
employeeOfTheMonth[JANUARY] = jamesBond;
Now you can have:
// "after", with enum and EnumMap
enum Month { JANUARY, ... }
Map<Month, Employee> employeeOfTheMonth = ...
employeeOfTheMonth.put(Month.JANUARY, jamesBond);
In Java, enum
is a very powerful abstraction which also works well with the Java Collections Framework.
enum
instead of int
constantsEnumSet
instead of bit fieldsEnumMap
instead of ordinal indexingEnumSet
and EnumMap
usageIn Java enums don't hold any other values by default. You'll have to create a private field to store one. Try something like this
enum Direction {
NORTH_WEST(0x0C),
NORTH(0x10),
...
private final int code;
private Direction(int code) {
this.code = code;
}
}
Add getter if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With