简体   繁体   English

为什么拒绝在静态块内的try-catch块中分配最终字段?

[英]Why assigning a final field in a try-catch block within a static block is rejected?

I'm trying to write following class: 我正在尝试编写以下课程:

public class Secteur {
    private final static int num_secteur;
    static{
            try {
                num_secteur = SecteurDAO.getCurrNumSecteur();
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }

    }
}  

But I'm having following warning : 但是我有以下警告:

The blank final field num_secteur may not have been initialized 空白的最终字段num_secteur可能尚未初始化

The SecteurDAO.getCurrNumSecteur() method accesses the database in order to get the integer that will be set to num_secteur field. SecteurDAO.getCurrNumSecteur()方法访问数据库,以获取将设置为num_secteur字段的整数。
What is the problem with my code ? 我的代码有什么问题?

The call to SecteurDAO.getCurrNumSecteur() might fail (throw an exception) and thus there's no value to assign to num_secteur . 调用SecteurDAO.getCurrNumSecteur()可能会失败(引发异常),因此没有赋值给num_secteur值。 Hence it might not be initialized in case of an exception. 因此,在发生异常的情况下可能无法初始化。

To fix that you might either want to initialize it with some special value (eg -1) or set that value in the catch-block. 要解决此问题,您可能想要使用一些特殊值(例如-1)对其进行初始化,或者在catch块中设置该值。

Oh, and remove the final keyword, otherwise you'd get the error message "The final field num_secteur may already have been assigned". 哦,并删除final关键字,否则您将收到错误消息“可能已经分配了final字段num_secteur”。 That's because you can only ever assign a value to a final field once and even if you know that assignment can't happen in case of an exception (because the exception will be thrown before) the compiler can't know that for sure and hence tells you that the variable might have been already assigned. 那是因为您只能将一个值赋给final字段一次,即使您知道在发生异常的情况下也不会发生赋值(因为该异常将在之前抛出),编译器无法确定并因此告诉您该变量可能已被分配。

As as been explained, if there is an exception, the variable might not be assigned at the end of the static initializer. 如前所述,如果存在异常,则可能不会在静态初始化程序的末尾分配该变量。

You might think of assigning to the variable before the try block, or after the catch block, but that can lead to 2 assignments to the variable, so that's disallowed. 您可能会想到在try块之前或catch块之后分配变量,但这可能导致对该变量进行2次分配,因此是不允许的。

You might think of assigning an invalid value to the variable in the catch block, but that still can lead to 2 assignments to the variable. 您可能会想到为catch块中的变量分配一个无效值,但这仍然可能导致对该变量进行2次分配。

The best way to assign this variable exactly once, and still keep num_secteur final , is to use a temporary variable and assign it to num_secteur in a finally block. 最好只分配一次此变量并保持num_secteur final的最佳方法是使用一个临时变量,并在finallynum_secteur其分配给num_secteur

private final static int num_secteur;
static
{
    int test = -1;  // Invalid value.
    try {
        test = SecteurDAO.getCurrNumSecteur();
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    } finally {
        // Only assigned here, exactly once.
        num_secteur = test;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM