Why is this enum not valid ?
enum Type{
MPEG-2=2,PASSED_PIDS_ID=3,DVB=4,ATSC=5,NA=6,UNDETERMINED=7
}
You have a couple of syntax errors, one in the first identifier (the - is invalid in a variable name) and also in how you're setting your values. You don't use = in an enum, but you can use a constructor instead. Try this:
enum Type {
MPEG2(2), PASSED_PIDS_ID(3), DVB(4), ATSC(5), NA(6), UNDETERMINED(7);
private final int value;
Type(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Remove the =<number>, it is invalid. Also, the minus sign in the first type is not valid syntax.
The Enum has a method called ordinal() which returns the order of the Enum, but it is not recommended to rely on that, should you add any new Enum typed in the future, then the Enums after it in the list will all have an ordinal of one higher.
Should you wish to include some more data, then you can have a constructor, which you could use as example:
public enum Fruit {
APPLE("Green"),
BANANA("Yellow");
private final String colourDescription;
Fruit(String colourDescription) {
this.colourDescription = colourDescription;
}
public String getColourDescription() {
return colourDescription;
}
}
So for your example, it may be best to have no extra information, or create a constructor as above and pass a number in that way.
ordinal() - will be better to use the constructor in that case.Fruit(...){ rather than private Fruit(...){)new on an enum