I'm converting some C# code to Java and it contains the using
statement. How should I replicate this functionality in Java? I was going to use a try
, catch
, finally
block but I thought I'd check with you guys first.
That's correct. A C# using block is just syntactic sugar for that anyway. The closest Java equivalent to IDisposable is Closeable
.
There is a proposal (which is partially committed already), called Automatic Resource Management, for adding similar functionality to Java 7. It would use try-finally behind the scenes, and proposes creating a new Disposable interface (which would be a superinterface of Closeable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With