简体   繁体   中英

How do I create a method similar to onDestroy in a simple non-activity java class?

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM