java - Behaviour of try-with-resources with closable parameters -


does java-7's try-with-resources require closable assigned directly variable? in short, block of code...

    try (final objectinputstream ois = new objectinputstream(             new bytearrayinputstream(data))) {         return ois.readobject();     } 

equivalent block?...

    try (final bytearrayinputstream in = new bytearrayinputstream(data);          final objectinputstream ois = new objectinputstream(in)) {         return ois.readobject();     } 

my understanding of section 14.20.3 of java language specification says not same , resources must assigned. surprising common usage standpoint , can't find documentation warning against pattern.

the 2 blocks not equivalent in sense won't generate same code. since objectinputstream.close() call close() on bytearrayinputstream passed it, first block fine.

edit: forgot unlike reasonable constructions new bufferedinputstream(new *inputstream(...)), objectinputstream constructor reads stream pass , reasonably throw exception. reason i'd recommend second block, not first block.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -