Given:
public class Main { class MyClass implements AutoCloseable { public void close() throws Exception { System.out.print("l"); } // w w w .j a v a 2s . co m } class MyClass2 implements AutoCloseable { public void close() throws Exception { System.out.print("g"); } } public static void main(String[] args) throws Exception { new Main().run(); } public void run() throws Exception { try (MyClass l = new MyClass(); System.out.print("t"); MyClass2 g = new MyClass2();) { System.out.print("2"); } finally { System.out.print("f"); } } }
What is the result?
A. 2glf
B. 2lgf
C. tglf
D. t2lgf
E. t2lgf
F. None of the above; main() throws an exception
G. Compilation fails
G is correct.
System.out.println cannot be in the declaration clause of a try-with-resources block because it does not declare a variable.
If the println was removed, the answer would be A because resources are closed in the opposite order they are created.