title: Does Java support RAII/deterministic destruction?
body: <p>It's been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a <code>finally</code> block and call the cleanup method in there.</p>
  
  <p>By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), the class implementor would define a destructor function that performs the cleanup whenever an object of that class goes out of scope.  The advantage of this approach is that the user of the object can't forget to clean it up -- the destructor gets called automatically, even if an exception is thrown.  This approach goes by the fairly awful name of RAII -- "Resource Acquisition Is Initialisation".</p>
  
  <p>It's been my experience that doing things "the RAII way" has saved me a lot of mental overhead in terms of not having to worry about whether and when resource deallocations occur.  We are considering using Java for a medium-sized project, and I'm wondering if some sort of deterministic destruction is among the many new features added to the language since I last looked at it.  (I'm hopeful as my complaint that "Java has no RAII" was rebuked <a href="https://stackoverflow.com/questions/457884/what-are-the-things-java-got-wrong#475089">on this thread</a>, but so far I haven't been able to find any details by googling.)</p>
  
  <p>So if someone could point me to some introductory material on how to go about this in Java that would be great!</p>