title: How do I create a method similar to onDestroy in a simple non-activity java class?
body: <p>I'd like to perform one or two actions when the instance of the class is shut down or destroyed.  I'm looking for something similar to the onDestroy in an activity.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've added my code where I indicate how I serve back a SQLiteDatabase from my Helper class. I use the finalize code to ensure the database is closed.</p>
  
  <pre><code>public class PMDBDatabase {
  
      private static SQLiteDatabase DataBase = null;
      private static PMDBHelper dbHelper = null;
  
      public SQLiteDatabase getDatabase(Context ctx) throws SQLException {
          if (DataBase == null) {
              dbHelper = PMDBHelper.getInstance(ctx);
              DBOpen();
          } else
              if(!DataBase.isOpen())
                  DBOpen();
          return DataBase;
      }
  
      private void DBOpen() throws SQLException {
          DataBase = dbHelper.getWritableDatabase();
      }
  
      public void close(){
          if(DataBase != null)  DataBase.close();
      }
  
      protected void finalize() throws Throwable {
          try {
              close();
          } finally {
              super.finalize();
          }
      }
  }
  </code></pre>
  
  <p>Could you please help this newbie to Java/Android programming and indicate whether the implementation of finalize is correct?</p>
  
  <p>Thanks a lot for your time,</p>
  
  <p>Jean</p>