What is the best way to clean up an Object in Java?

Viewed 27716

We don't have any destructor in Java as we have in C++.

Q1. How should we clean up any Object in java.

Q2. Is there any alternative to a finally block.

Q3. Sometimes we have to call explicitely initialization/termination of a third party code from our class e.g.

public classs MyClass{
    public MyClass(){
        ThirdPartyInitialize();
    }

    protected void finalize(){
        ThirdPartyTerminate();
    }
}

Is this the correct way?

8 Answers
Related