前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

只需要在case :后面加上{ }中括号,使变量成为局部变量就好了

报错的显示

FileBusinessOrigin fileBusinessOrigin = FileBusinessOrigin.AUTH;
switch (fileBusinessOrigin) {
    case CONTRACT:
        String abc = "";
        break;
    case AUTH:
        //报错
        // String abc = "";
        break;
    default:
        break;
}

解决方案:加上{}

switch (fileBusinessOrigin) {
    case CONTRACT: {
        String abc = "";
        System.out.println(abc);
        break;
    }
    case AUTH: {
        String abc = "";
        System.out.println(abc);
        break;
    }
    default:
        break;
}

ContactAuthor