Does Java support RAII/deterministic destruction?

EDIT: The answer below was written in early 2009, when Java 7 was very much still in flux.

While Java still doesn’t provide guarantees around finalization timing, it did gain a feature like C#’s using statement: the try-with-resources statement.


No, Java hasn’t changed at all in that respect. You still need to use try/finally.

There’s been discussion of adding the equivalent of C#’s “using” statement (which is syntactic sugar over try/finally) to Java, but I don’t think that’s going to be part of Java 7 any more. (Most of the language improvements seem to have been dropped.)

It’s worth understanding that there are reasons why deterministic destruction hasn’t been implemented in Java and .NET in the form of a reference-counted garbage collector, by the way – that a) impacts performance and b) fails with circular references. Brian Harry wrote a detailed email about this – it’s about .NET and it’s rather old, but it’s well worth a close read.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)