finalize()

Student:              What happened Sir? No blogs now a days?

Sir:                          I fell down from steps and there is a fracture on my right hand. So I cannot write till the plaster is removed.

Student:              Oh…I’m sorry. Are you okay now?

Sir:                          I’m fine. Only some difficulties as I cannot use my right hand now.

Student:              Take care Sir. I will pray for you.

Sir:                          Thank you so much.

Student:              Sir, I went for an interview yesterday. I could answer everything except one. They asked me about final, finally and finalize. I explained final and finally well. But I didn’t know what is finalize.

Sir:                          You don’t have finalize() in your jTKeyword ?

Student:              No Sir, you have not taught me that.

Sir:                          I had mentioned about that in the class, may be you wouldn’t have come on that day.

Student:              Yeah may be, sorry Sir. What is this finalize Sir?

Sir:                          finalize() is a method in Object class. It’s signature is protected void finalize(). This is the official statement: “Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.”.

Student:              Okay, so how does it help us?

Sir:                          You can override the finalize() method and write the code for any clean up activity like closing an opened connection/stream, releasing any system resources etc. And this finalize() method is called only once by the garbage collector thread.

Student:              That’s really great. So one will be relieved from resource leakages if all the clean up code is placed in finalize() method.

Sir:                          Don’t get excited. It’s not a good practice to rely completely on finalize() for doing all cleanup activities.

Student:              Why is it so Sir?

Sir:                          As long as the object is accessible calling finalize() may not happen at all. Also GC(Garbage Collector) thread is not guaranteed to run at any specific time. There is no guarantee that an object will be collected immediately after becoming eligible for garbage collection. So don’t rely completely on finalize() for releasing any critical resource.

Student:              Okay Sir. What should be the approach in such cases ?

Sir:                          close the stream/connection etc in the finally block. Also use finalize() as another option. Use finalize() as a last attempt. Never use it as the only attempt.

Student:              Thank you Sir. Very useful information. Take care of your hand. When will be the plaster on your hand removed?

Sir:                          You are welcome.  Plaster will be removed in 5 days. Hope the doctor will call the finalize() method during the removal. 😀

 

 

Saju Pappachen

 

About the author: Saju Pappachen is the Chief Consultant of jThread IT Training & Consultancy. He has more than 20 years of experience in Java technology.

He can be reached at saju@jthread.com

31 thoughts on “finalize()

Leave a Reply

Your email address will not be published. Required fields are marked *