You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中enum配合switch case使用报常量表达式错误如何解决

报错原因

Java 语法规定 switch-case 结构中 case 后跟随的值必须是编译期可确定的常量,而枚举的 name() 方法返回值是程序运行时调用方法得到的结果,不属于编译期常量,因此会触发 Constant expression required 编译错误。

可用解决方案

你可以根据自己的代码习惯选以下任意一种方案修复:

  • 方案1:最小改动,将switch-case替换为if-else判断
    直接用字符串的equals()方法做匹配即可,不需要额外改造其他代码:
    public static int getIntegerValue(Context context, String key) {
        initializeSharedPreferences(context);
        if (Keys.COUNTRY_ID.name().equals(key) || Keys.CURRENCY_ID.name().equals(key)) {
            return sharedPreferences.getInt(key, 5);
        }
        return -1;
    }
    
  • 方案2:保留switch结构,先将字符串转换为枚举实例再判断
    枚举本身是支持作为switch-case的判断类型的,你可以先把传入的字符串key转成对应枚举,再走switch逻辑:
    public static int getIntegerValue(Context context, String key) {
        initializeSharedPreferences(context);
        Keys targetKey;
        try {
            targetKey = Keys.valueOf(key);
        } catch (IllegalArgumentException e) {
            // 传入的key不存在对应枚举,直接返回默认值
            return -1;
        }
        switch (targetKey) {
            case COUNTRY_ID:
            case CURRENCY_ID:
                return sharedPreferences.getInt(key, 5);
            default:
                return -1;
        }
    }
    
    注意:该方案要求传入的字符串和枚举的name()返回值完全一致,否则会抛出参数非法异常,必须做异常捕获。
  • 方案3:优化枚举设计,从根源避免字符串硬编码问题
    更推荐的做法是给枚举绑定固定的存储key字符串,重载SP的读写方法支持直接传入枚举参数,既保证类型安全,也不会因为后续修改枚举类的常量名导致本地已存储的SP数据无法读取:
    public class SharedPreferencesManager {
    
        private static SharedPreferences sharedPreferences;
        private static SharedPreferences.Editor editor;
    
        private static void initializeSharedPreferences(Context context) {
            if (sharedPreferences == null)
                sharedPreferences = context.getSharedPreferences("SharedPreferencesManager", Context.MODE_PRIVATE);
        }
    
        private static void initializeEditor() {
            if (editor == null)
                editor = sharedPreferences.edit();
        }
    
        public static void setValue(Context context, Keys key, int value) {
            initializeSharedPreferences(context);
            initializeEditor();
            editor.putInt(key.getSpKey(), value);
            editor.apply();
        }
    
        public static int getIntegerValue(Context context, Keys key) {
            initializeSharedPreferences(context);
            switch (key) {
                case COUNTRY_ID:
                case CURRENCY_ID:
                    return sharedPreferences.getInt(key.getSpKey(), 5);
                default:
                    return -1;
            }
        }
    
        public enum Keys {
            COUNTRY_ID("sp_country_id"),
            COUNTRY_NAME("sp_country_name"),
            CURRENCY_ID("sp_currency_id"),
            CURRENCY_NAME("sp_currency_name");
    
            private final String spKey;
    
            Keys(String spKey) {
                this.spKey = spKey;
            }
    
            public String getSpKey() {
                return spKey;
            }
        }
    
    }
    
    后续调用时直接传入枚举值即可,比如setValue(context, Keys.COUNTRY_ID, 100),不需要手动传字符串,能避免很多拼写错误。

内容的提问来源于stack exchange,提问作者Taha Sami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 15:09:21