Not really, but you can do something like
{ MyClass m = reallyLongExpressionReturningAnObject(); m.myMethod1(); m.myMethod2(); m.myMethod3(); }
I don’t know Visual Basic, but a function in some languages with similar syntax has one additional advantage, besides saving, in order to type expression more: it automatically closed the object at the end of the block, even if some exception occurred in the block.
There was some discussion about adding this word to Java, and it looks like it was in JDK 7. The syntax would be a bit, however, as an extension of the try statement. Then you can write
try (BufferedReader in = new BufferedReader(new FileReader(...))) { String line; while((line = in.readLine()) != null) { list.add(line); } }
... and Reader (and all threads wrapped by it) will be automatically closed after reading (or exception).
This will work for all objects implementing the (new) java.lang.AutoClosable interface. If an exception is selected in the block itself and another exception occurs during cleaning, this other exception is suppressed and added to the original exception with addSuppressed(...) .
You still have to call the object a name (variable) inside the block.
source share