简体   繁体   English

了解 Java 9 中的 try-with-resources 增强

[英]Understanding try-with-resources enhancement in Java 9

Instead of initializing resource in try with resources block like this:而不是像这样使用资源块try中初始化资源

try(FileWriter fw = new FileWriter ("java.txt")) {
    //some operation
}catch (IOException ioe) {
    ioe.printStackTrace ();
}

, we can now just pass a reference to try with resources block , which we would like to auto close , like this: ,我们现在可以只传递一个引用来尝试使用我们希望自动关闭的资源块,如下所示:

FileWriter fw = new FileWriter ("java.txt");

try(fw) {
    //some operation
}catch (IOException ioe){
    ioe.printStackTrace ();
}

And that part is clear so far.到目前为止,这部分很清楚。 My question is why the code I will show in the next example doesn't work?我的问题是为什么我将在下一个示例中显示的代码不起作用? What difference does it have from the previous example, which compiled just fine?它与前面的示例有什么不同,后者编译得很好?

public class Test {

    private FileWriter fileWriter;

    public Test (FileWriter fileWriter) throws IOException {
        this.fileWriter = fileWriter;
    }

    {
        try(fileWriter) { //compile error:
//"Variable used as a try-with-resources resource should be final or effectively final""
        }catch (IOException ioe){
            ioe.printStackTrace ();
        }
    }

}

Can someone help me understand what does this message even mean?有人可以帮助我理解这条消息的含义吗? I have no clue what might be the issue here.我不知道这里可能是什么问题。 I also tried putting final in fileWriter variable declaration, which didn't solve the problem.我还尝试将final放在fileWriter变量声明中,但这并没有解决问题。

I get the following compiler error, not matching yours:我收到以下编译器错误,与您的不匹配:

The blank final field fileWriter may not have been initialized

This is because you're referring to fileWriter in an instance initializer , which executes after superclass constructor calls and explicit constructor calls but before any remaining code in a constructor.这是因为您在实例初始化程序中引用了fileWriter ,它在超类构造函数调用和显式构造函数调用之后但在构造函数中的任何剩余代码之前执行。 According to the JLS, Section 12.5, "Creation of New Class Instances" :根据JLS,第 12.5 节“创建新类实例”

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.将构造函数的参数分配给此构造函数调用新创建的参数变量。

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps.如果此构造函数以同一类中另一个构造函数的显式构造函数调用(第 8.8.7.1 节)开始(使用 this),则评估参数并使用相同的五个步骤递归处理该构造函数调用。 If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason;如果构造函数调用突然完成,那么这个过程也会因为同样的原因突然完成; otherwise, continue with step 5.否则,继续第 5 步。

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this).此构造函数不以对同一类中另一个构造函数的显式构造函数调用开始(使用 this)。 If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super).如果此构造函数用于 Object 以外的类,则此构造函数将以显式或隐式调用超类构造函数(使用 super)开始。 Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps.使用这五个相同的步骤递归地评估超类构造函数调用的参数和过程。 If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason.如果该构造函数调用突然完成,则此过程出于同样的原因而突然完成。 Otherwise, continue with step 4.否则,继续第 4 步。

  4. Execute the instance initializers and instance variable initializers for this class , assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class.执行这个类的实例初始化器和实例变量初始化器,将实例变量初始化器的值分配给相应的实例变量,按照它们在类的源代码中以文本形式出现的从左到右的顺序。 If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception.如果执行这些初始化程序中的任何一个导致异常,则不会处理进一步的初始化程序,并且此过程会突然完成并出现相同的异常。 Otherwise, continue with step 5.否则,继续第 5 步。

  5. Execute the rest of the body of this constructor .执行此构造函数的其余部分 If that execution completes abruptly, then this procedure completes abruptly for the same reason.如果该执行突然完成,那么这个过程也会出于同样的原因突然完成。 Otherwise, this procedure completes normally.否则,此过程将正常完成。

( bold emphasis mine) 粗体强调我的)

This means that your constructor hasn't assigned anything to fileWriter yet, causing the actual error.这意味着您的构造函数尚未为fileWriter分配任何内容,从而导致实际错误。

Either place the try-with-resources block in the constructor after the initialization of fileWriter , or move it into a method.fileWriter初始化之后将 try-with-resources 块放在构造函数中,或者将其移动到方法中。

It essentially means that Java wants you to ensure that the variable you pass to try-with-resources (in this case fileWriter ) will not be changed once it is assigned.这实质上意味着 Java 希望您确保传递给 try-with-resources(在本例中为fileWriter )的变量在分配后不会更改。 If the variable could be changed (say within the try block itself) then all sorts of undefined behaviour might arise.如果变量可以改变(比如在 try 块本身内),那么可能会出现各种未定义的行为。

From a code perspective, to make this work, add final in your declaration of fileWriter :从代码的角度来看,要完成这项工作,请在fileWriter的声明中添加final

public class Test {

    private final FileWriter fileWriter;  // add 'final'

    public Test (FileWriter fileWriter) throws IOException {
        this.fileWriter = fileWriter;
    }
 
    // ... in some other method
    private void foo() {
        try(fileWriter) {
        } catch (IOException ioe){
            ioe.printStackTrace ();
        }
    }

}

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

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