switch case statement error: case expressions must be constant expression

后端 未结 9 1423
予麋鹿
予麋鹿 2020-12-04 07:51

My switch-case statement works perfectly fine yesterday. But when I run the code earlier this morning eclipse gave me an error underlining the case statements in color red a

9条回答
  •  醉话见心
    2020-12-04 08:16

    It was throwing me this error when I using switch in a function with variables declared in my class:

    private void ShowCalendar(final Activity context, Point p, int type) 
    {
        switch (type) {
            case type_cat:
                break;
    
            case type_region:
                break;
    
            case type_city:
                break;
    
            default:
                //sth
                break;
        }
    }
    

    The problem was solved when I declared final to the variables in the start of the class:

    final int type_cat=1, type_region=2, type_city=3;
    

提交回复
热议问题