Reputation: 1059
Instead of initializing resource in try with resources block like this:
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.
Upvotes: 1
Views: 224
Reputation: 178293
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. According to the JLS, Section 12.5, "Creation of New Class Instances":
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
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. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using 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). 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.
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.
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.
Either place the try-with-resources block in the constructor after the initialization of fileWriter
, or move it into a method.
Upvotes: 3
Reputation: 2502
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. If the variable could be changed (say within the try block itself) then all sorts of undefined behaviour might arise.
From a code perspective, to make this work, add final
in your declaration of fileWriter
:
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 ();
}
}
}
Upvotes: 2