朋友,我很困惑,在编码时,我不小心在方法中插入了一个大括号

List<EmpQualificationLevelTo> fixedTOs = employeeInfoFormNew.getEmployeeInfoTONew().getEmpQualificationFixedTo();
if(fixedTOs != null && !fixedTOs.isEmpty())
{
    Iterator<EmpQualificationLevelTo> it = fixedTOs.iterator();
    while(it.hasNext())
    {
        EmpQualificationLevelTo fixedTO = it.next();
        FormFile eduDoc = fixedTO.getEducationDoc();
        if((eduDoc != null && eduDoc.getFileName() != null && !eduoc.getFileName().isEmpty()) && (fixedTO.getQualification() != null && !fixedTO.getQualification().isEmpty())) {
            errors.add("error", new ActionError( "knoledgepro.employee.education.uploadWithoutQualification"));
        }
        {

        }
    }
}


您可以在while循环内的if块下面看到它。谁能提供帮助,为什么它没有给出任何编译时错误?

最佳答案

这不是instance initializer。实例初始化器在类或枚举体中声明,而不是在方法中声明。

这只是一个空的block:不必要,但仍然合法。

空块和空语句可以安全删除:

{
    ;
    ;;
    //this block compiles successfully
    ;{}
}


[更新]:从技术上讲,块可用于分隔作用域。例如:

{
String test = "test";
//do something with test
}
{
String test = "test2";
//do something with test
}


在这种情况下,具有相同名称的变量将在单独的作用域中声明。

10-08 03:04