Hi In a servlet, is destroy() called before or after finalize()?
destroy() is called first. destroy() will be called by the servlet-container at the time the servlet will be shut-down. finalize() is called by the JVM before the garbage-collector claims the objects (and isn't guaranteed to be called at all).
Also, finalize() may or may not be called. Don't ever rely on that happening.
finalize() method will called when object goes out of scope and eligible for garbage collection. So destroy() method definitely called before it when object doesn't got out of scope.
If you want to free resources on a certain time, don't depend on external code like the servlet container or the JVM to do so. Do your resource allocation and de-allocation as explicit as possible. Nasty bugs can be the result from depending on external code to clean up after you.