Java enum: Refactoring operator statements "constant expression required" compilation error?

I have a class declaring constants for my application

public class GroupConstants { .. public static final int INTEGER_VALUE = 1; public static final int LONG_VALUE = 2; public static final int STRING_VALUE = 3; .. } 

The code has a set of switch statements

 private static Object getValue(String stringValue, Parameter parameter) throws InvalidPatternException { Object result=null; switch (parameter.getDataType()) { case GroupConstants.STRING_VALUE: // String value result=stringValue; break; case GroupConstants.INTEGER_VALUE: // Long value case GroupConstants.LONG_VALUE: case GroupConstants.BOOLEAN_VALUE: case GroupConstants.DATE_VALUE: .. } 

I want to reorganize constant int values ​​that will be represented by an enumeration

 public enum DataType { UNKNOWN_VALUE(0,"unknown"), INTEGER_VALUE(1,"integer"), LONG_VALUE(2,"long"), STRING_VALUE(3,"string"), BOOLEAN_VALUE(4,"boolean"), .. } 

so my code might look like this:

 @Deprecated public static final int INTEGER_VALUE = DataType.INTEGER_VALUE.getId(); 

and overtime, I can change my switch statements. When I change the static final reference to int to indicate an enumeration, all of my switch statements are interrupted.

 [javac] /home/assure/projects/tp/main/src/a/b/c/DDDDDManagerBean.java:1108: constant expression required [javac] case GroupConstants.INTEGER_VALUE: [javac] ^ [javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:203: constant expression required [javac] case GroupConstants.INTEGER_VALUE: [javac] ^ [javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:268: constant expression required [javac] case GroupConstants.INTEGER_VALUE: [javac] ^ [javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:316: constant expression required [javac] case GroupConstants.INTEGER_VALUE: [javac] ^ [javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:436: constant expression required [javac] case GroupConstants.INTEGER_VALUE: 

I don’t want to be forced to change all the switches, so is there any clean work there?

+6
java
source share
4 answers

This will not work. The problem is that calling getId() means that the constant expression is not a JLS compile-time constant expression. See JLS 15.28 Constant Expressions for details and you will see that method calls are not allowed in constant expressions.

I don’t think there is any workaround other than a large-scale change of all switch statements. But I would not worry. Your IDE should help you find and replace all occurrences of old constants.

Followup

The following code from your comment will not work:

 private int INTEGER_VALUE_HOLDER = DataType.INTEGER_VALUE.getId(); public static final int INTEGER_VALUE = INTEGER_VALUE_HOLDER; 

First, INTEGER_VALUE_HOLDER not a "constant variable" as defined in JLS 4.12.4 Final Variables . Therefore, not INTEGER_VALUE .

Secondly, an initializer expression for static cannot refer to this , and INTEGER_VALUE_HOLDER is really a different way of saying this.INTEGER_VALUE_HOLDER .

+5
source share

Java has built-in enumeration support 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; 
+8
source share

Try to get rid of the GroupConstants. prefix GroupConstants. in your case statements. For reasons completely incomprehensible to me, it does not accept the same constant if it has a prefix with the class name.

So instead

  case GroupConstants.STRING_VALUE: 

try:

  case STRING_VALUE: 

It may require static import to compile it.

+2
source share

in eclipse the IDE is simple, in the CTRL + 1 clause and the switch switch clause - if-else clause http://tools.android.com/tips/non-constant-fields

+1
source share

All Articles