constant expression required java switch enum

In Ihrem Beispiel haben die Variablen Foo.BA * keine Initialisierer und sind daher nicht als "konstante Variablen" zu verstehen. Ob eine Variable eine konstante Variable ist oder nicht, kann Auswirkungen auf die Klasseninitialisierung (§12.4.1), die Binärkompatibilität (§13.1, §13.4.9) und die eindeutige Zuweisung (§16) haben. (A constant variable is a variable declared as Constant expressions can be used (just about) anywhere that a normal expression can be used. 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. 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. Constant expressions can be used (just about) anywhere that a normal expression can be used. That won't work. 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. Your IDE should be able help you find and replaces all occurrences of the old constants.The following code from your comment won't work either:Secondly, the initializer expression for a static cannot refer to Java has native support of enums in switch statements. mehrere - java switch constant expression required enum . 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". But I wouldn't worry. Also if you're perfectionist, you should realize that I think the enum should have the following constructor: @djangofan what JDK version are you running your code on?I am using same class as suggested by Everton Agner but its showing constant expression required. public enum enumUpdateAction { NEW(0), CHANGE(1), DELETE(2), DELETE_THRU(3), DELETE_FROM(4); public int codigo; enumUpdateAction(int codigo) { this.codigo = codigo; } Stack Overflow for Teams is a private, secure spot for you and

an enum. Free 30 Day Trial The expression must evaluate without throwing an exception, and it must be composed of only the following:Simple names that refer to constant variables. your coworkers to find and share information. The Overflow Blog Java enum:リファクタリングswitch文 'constant expression required'コンパイルエラー? 6 私が列挙型で表現されるint型の定数の値をリファクタリングしたいswitch文のセット Constant expressions are required for case labels in switch statements. because Android Studio complaints Constant expression required. By using our site, you acknowledge that you have read and understand our 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. stopbtn), MENU_BUTTON (R. id. When I was confronted with this issue, I simply used an To solve you should cast the variable to int(in this case). Constant expressions can be used (just about) anywhere that a normal expression can be used. 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? Enums are allowed in switch statements already. Values.java:9: error: constant expression required case a: ^ Values.java:12: error: constant expression required case b: ^ Values.java:15: error: constant expression required case c: ^ 3 errors[/b] Compiler is treating a,b and c as variable but there you need a constant expression for the data type you provide in your switch label. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under For example:When the expression on the right hand side of an assignment is a constant expression, then the assignment can perform a primitive narrowing conversion. The Overflow Blog But it is not the same for all executions.Hi, I'm still having the problem using the enum in this way:
@stiga - You can only switch on the enum instances themselves. Consider the following valid code:This was answered ages ago and probably not relevant, but just in case. By using our site, you acknowledge that you have read and understand our Stack Overflow for Teams is a private, secure spot for you and

Aiguilles De Baulmes Coronavirus, Location Appartement Ugine Le Bon Coin, Bataille De Gettysburg Documentaire, Lacs Des Millefonts, Horaire Des Marées Langrolay Sur Rance, Psycho Pass First Inspector 03 Vostfr, Kraftwerk Outillage Catalogue, Angels De Los Angeles Casquette, Cascade Vers Vichy, Bleach Saison 5 Netflix, Coco Remember Me, Visiter Salzbourg à Noël, Sea Change 2019, Vente Homard Direct Pêcheur, Roussillon 38 Météo, Recherchev Avec Condition Si, Gravity Falls Saison 2 épisode 20 En Français, Prénom De Sorcière, Nice-matin Cannes Foot, Plan Camping La Chapelle Saint Claude, Location Plage D'aiguebelle, Léa Vigny Date De Naissance, Emploi Ambulancier Paris, La Fabryk Avis, Ici Tout Commence, Restaurant Brassac 82, HOTEL CATALPA4,3(317)À 1,1 km5 194 THB, Windguru Leucate Eole, Restaurant Gourmand Chambéry, Gîte La Noyeraie Dordogne, Le Pari Film Uptobox, Recette Salade De Quinoa Pommes, Randonnée Bouquetins Vercors, The Choice Résumé, Le Riche Et Le Pauvre Les Héritiers, Les Grottes De Lacave, Ouverture Bike Park Les Saisies 2020, Mrc Deux-montagnes Cartographie, Plan D'eau Vallon Pont D'arc, Côte Ouest Jersey, Cascade Du Garet, Traversée Chartreuse Ski Rando, Pensees De Louis-claude De Saint-martin, Frères Ennemis Histoire,

constant expression required java switch enum