java - Try-with-resources when calling super constructor -


is there way of using try-with-resources when opening inputstream in constructor , passing super constructor?

basically want this:

public class {     public a(inputstream stream) {         // stream don't close since didn't open     } }  public class b {     public b(file file) {         // open stream need ensure it's closed         try (fileinputstream stream = new fileinputstream(file)) {             super(new fileinputstream(file));         }     } } 

but, of course, since super must first statement in constructor isn't allowed. there way of achieving this?

consider using static factory method instead of using constructor directly. make @ least b's constructor private, , create method such as

private b(inputstream is) {     super(is);     // whatever else needed }  public static b newinstance(file file) {     b result;     try (fileinputstream stream = new fileinputstream(file)) {         result = new b(stream);     }     // further processing     return result; }