What is the output of the following application?
package mypkg;// ww w . ja va2s . co m import java.io.Closeable; public class Main { class Printer implements Closeable { // r1 public void print() { System.out.println("This just in!"); } public void close() {} } public void printHeadlines() { try {Printer p = new Printer()} { // r2 p.print(); } } public static void main(String[] headlines) { new Main().printHeadlines(); // r3 } }
C.
The class does not compile because in line r2, brackets {} are used instead of parentheses () in the try-with-resources statement, making Option C the correct answer.
If this line was fixed to use parentheses()
, then the rest of the class would compile without issue and print This just in! at runtime, making Option A the correct answer.