I have a class declaring constants for my app
public class GroupConstants {
..
public static final int INTEGER_VALUE = 1;
public static final int
in eclipse IDE is simple ,in switch sentence CTRL + 1 and convert switch sentence - if-else sentence http://tools.android.com/tips/non-constant-fields
Try getting rid of the GroupConstants.
prefix in your case statements. For reasons completely unclear to me, it doesn't accept the same constant if it's prefixed with class name.
So instead of
case GroupConstants.STRING_VALUE:
please try:
case STRING_VALUE:
You might need a static import to make it compile.
That won't work. The problem is that the getId()
call means that the constant's value expression is not a compile-time constant expression according to the JLS. Refer to JLS 15.28 Constant Expressions for the gory details, and you will see that method calls are not allowed in constant expressions.
I don't think there is any workaround, apart for a large-scale change to all of the switch statements. But I wouldn't worry. Your IDE should be able help you find and replaces all occurrences of the old constants.
FOLLOWUP
The following code from your comment won't work either:
private int INTEGER_VALUE_HOLDER = DataType.INTEGER_VALUE.getId();
public static final int INTEGER_VALUE = INTEGER_VALUE_HOLDER;
Firstly, INTEGER_VALUE_HOLDER
is not a "constant variable" according to the definition in JLS 4.12.4 Final Variables. Therefore neither is INTEGER_VALUE
.
Secondly, the initializer expression for a static cannot refer to this
, and INTEGER_VALUE_HOLDER
is really another way of saying this.INTEGER_VALUE_HOLDER
.
Java has native support of enums in switch statements. In your case you should say:
DataType type = ...;
switch (type) {
case UNKNOWN_VALUE
//do something
break;
case INTEGER_VALUE
//do something
break;
case LONG_VALUE
//do something
break;
case STRING_VALUE
//do something
break;
case BOOLEAN_VALUE
//do something
break;