title: java techniques for automatic resource release? "prompt cleanup"?
body: <p>I'm porting to Java some C++ code that uses the pretty common C++ trick of allocating an object on the stack (it happens to implement a UDP connection) which has some internal state information (here, a UDP socket).  While in scope the object is used to do various things (send and receive UDP messages).  The nice thing is that when control leaves this scope the object's destructor will be run and this can be counted upon to automagically cause the release the object's internal resources (in this case, I make sure the socket gets closed so that I can reuse its address and port numbers in other parts of my program).</p>
  
  <p>In trying to figure out how to do this I have learned that Java does not have a destructor, that "finalize" won't do what I want, etc.</p>
  
  <p>Surely there is some similarly clever technique of accomplishing the same thing in Java?  I realize I could add a "close()" method and try to make sure that it is always called at the appropriate time, and I further realize this would probably be more easily and reliably done in Java than in C++.  But do I really have to go that route?</p>