mehrere - java switch constant expression required enum . 问题描述 今天在代码中想对Java switch case 和枚举类型 Enum 对象进行联合 使用 ,但发现有Eclipse中异常提示信息:case expression s must be constant expression s,导致编译始终过不去。 Die Lösung ist einfach. farmazon3000 changed the title Make RxBleConnectionState compile time constant, e.g. By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. it's a legitimate workaroundprobably because it's an IF statement that we are specifically trying to avoid with a switchI down voted because the question here is not "how" to solve the problem, but "why" the problem occurred. Should it not be case DataType.INTEGER_VALUE: that should be in the compiler error?Based on what you've shows so far, I can't see what the problem is; this should work based on what I Your correct - a nasty work around is : private int INTEGER_VALUE_HOLDER = DataType.INTEGER_VALUE.getId(); public static final int INTEGER_VALUE = INTEGER_VALUE_HOLDER; But it defeats the purpose of the refactor.Your correct about using the Enum to drive the switch statement. 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. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesJust for clarification: This solves your error by making a static property final. Of course, it isn't truly a constant I suppose. Otherwise I get "constant expression required". Free 30 Day Trial because Android Studio complaints Constant expression required. your coworkers to find and share information. The expression must evaluate without throwing an exception, and it must be composed of only the following:Simple names that refer to constant variables. See Stephen's accepted answer. Yes, you can. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).In your example, the Foo.BA* variables do not have initializers, and hence do not qualify as "constant variables". I don't want to be forced to change all the switches yet, so its there a clean work around?I don't think there is any workaround, apart for a large-scale change to all of the switch statements. switch case statement error: case expressions must be constant expression (6) How about this other solution to keep the nice switch instead of an if-else: private enum LayoutElement {NONE (-1), PLAY_BUTTON (R. id.

That won't work. Consider the following valid code:This was answered ages ago and probably not relevant, but just in case.

By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. The Overflow Blog stopbtn), MENU_BUTTON (R. id. For example: Can I use an enum type in Java switch statement? Während sie aus der Perspektive eines beliebigen Codes konstant sind, der ausgeführt wird, nachdem die Felder initialisiert worden sind, sind sie keine Wir rufen eine Variable vom primitiven Typ oder Typ String auf, die final ist und mit einem Konstanten-Ausdruck zur Kompilierungszeit (§15.28) eine konstante Variable initialisiert wird. 4 Answers 4 解决方法. Dies wurde vor langer Zeit beantwortet und wahrscheinlich nicht relevant, aber nur für den Fall. We can use an enum with a switch case:Based on the class values from the enum can be mapped:Thanks for contributing an answer to Stack Overflow! Als ich mit diesem Problem konfrontiert wurde, habe ich einfach eine Ich habe diesen Fehler auf Android erhalten, und meine Lösung war nur zu verwenden: Um das Problem zu lösen, sollten Sie die Variable (in diesem Fall) auf int setzen. In my case the DataType represents an Object type, so rather than refactoring the switch to use Enum's, I plan to create an interface and move the logic with in the case statements to the object, allowing be to remove the switch statements from the code.What relevance is this? While they are constant from the perspective of any code that executes after the fields have been initialized, they are not a compile time constant in the sense required by the JLS; see §15.28 Constant Expressions for the specification of a constant expression 1. (Obviously switch statements are not required to be exhaustive.) @TonyEnnis - It depends on what you mean by truly constant. In Ihrem Beispiel haben die Variablen Foo.BA * keine Initialisierer und sind daher nicht als "konstante Variablen" zu verstehen. Featured on Meta Damit: Sie können eine Enumeration wie in diesem Beispiel verwenden: Weil das keine Kompilierzeitkonstanten sind.

I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn’t Foo.BA_ constant?