1

For the convenience I would like to use AutoCloseable interface to do some stuff with the object which should be finalized before its further usage.

The only one problem is that try-with-resources block is thing in itself, I mean that in general for obtaining reference to my AutoCloseable instance, I have to do something like this:

    MyCloseable mc;
    try (MyCloseable m = mc = new MyCloseable()) {
        m.doSomeStuff();
    }
    System.out.println(mc.getValue1());
    System.out.println(mc.getValue2());
    // ... and so on

Here this construction:

MyCloseable mc;
try (MyCloseable m = mc = new MyCloseable()) {

looks somewhat messy. So my question is more about design problem. I doubt for what reason java architects didn't envisage such usage of try-w-r block as:

MyCloseable mc = new MyCloseable();
try (mc) { ... }

so may be I shoudln't use it as I described above? If it's a good practice to do that?

EDIT I was asked to explain why does my question differ from Why is declaration required in Java's try-with-resource

The explanation is very simple: I'm not asking why?, but: If it's a good practice to do that?

Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 1
    I think [this](http://stackoverflow.com/q/13836486/1743880) is the same question. This will be possible in Java 9. – Tunaki Jan 15 '16 at 09:28
  • @Tunaki thanks, but I don't ask why it so, I ask, is it ok use it in such way from the design point of view – Andremoniy Jan 15 '16 at 09:29
  • 1
    Ah, so you're asking if it's a good practice to do that? – Tunaki Jan 15 '16 at 09:30
  • @Tunaki Yes, sure. But in any case your link is very useful – Andremoniy Jan 15 '16 at 09:31
  • @Tunaki But according to the fact that Java 9 will allow this, I guess that the answer will be **yes, it's ok** :) – Andremoniy Jan 15 '16 at 09:32
  • 1
    I guess so :). I can't find any objective reference for this. The initial enhancement request is [here](https://bugs.openjdk.java.net/browse/JDK-8068948) with the discussion [here](http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-January/030787.html). – Tunaki Jan 15 '16 at 09:37

2 Answers2

3

The intent of AutoCloseable is to automatically perform cleanups when it is no longer used.

So it would violate the principle of least astonishment to use the object after the try-with-resources block.

Frank Neblung
  • 3,047
  • 17
  • 34
  • I'm not agree cause of 2 reasons: 1) `java-9` will allow such usage; 2) `Stream` interface in `java-8` is also `AutoCloseable` but it is really unnecessary. – Andremoniy Jan 15 '16 at 10:03
0

Thanks to @Tunaki, he gave me very useful link to the related question: Why is declaration required in Java's try-with-resource

I was doubted not about why this restriction exists, but I was thinking that my idea of usage is something bad from the design point of view.

But according to the fact that Java-9 will allow such usage as

MyCloseable mc;
try (mc = new MyCloseable()) { ... }

what is exactly I'm looking for, so I think, the answer to my question will be: YES, IT'S OK

Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241