5

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.

EDIT

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.

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();
        }
    }
}

Could you please help this newbie to Java/Android programming and indicate whether the implementation of finalize is correct?

Thanks a lot for your time,

Jean

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
Jean
  • 137
  • 1
  • 13

3 Answers3

3

you can't do this because GC handle it automatically.

Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor. There is an inherited method called finalize, but this is called entirely at the discretion of the garbage collector.

finalize

It is possible to use something similar to a destructor in Java, the method Object.finalize() , which however does not work exactly like a standard destructor would.

Rustam
  • 6,485
  • 1
  • 25
  • 25
1

Just like the other answer said, there is no such a thing. But I myself have had the same need and here is how I approached it.

In my case, the class needing to do some final things had their own thread. If you implement Runnable you can override the close method and have it do your final things.

Cripto
  • 3,581
  • 7
  • 41
  • 65
0

Take a look at the AutoCloseable especially

    An object that may hold resources (such as file or socket handles)
 until it is closed. The close() method of an AutoCloseable object is
 called automatically when exiting a try-with-resources block for which
 the object has been declared in the resource specification header. This
 construction ensures prompt release, avoiding resource exhaustion
 exceptions and errors that may otherwise occur.

There is no guarantee when exactly finalize would be called. You are stimulated to write the code that will take care about resources by yourself and AutoCloseable is your best bet.

Observer
  • 710
  • 10
  • 14
  • Alex, thanks. As I keep my DB open all the time (it is a local DB), I only need the DB closed if the app goes to the background (the user uses other apps) and the system wish to free resources by killing the app. I believe that if the resources are to be GC-ed the finalize will fire and the DB will be closed. – Jean Sep 14 '15 at 13:37
  • If this is the only connection and a) you close it, then you need no special treatment as connection will be closed by it's own by it's implementation or by connection timeout event on OS level. b) the different story is to close connection on minimise. This require http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowStateListener.html Catching the events you will be able to open/close connection on your own. – Observer Sep 15 '15 at 15:19