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
good one sir😃
As always an interesting way to understand the topic👍
Thank you Fathima.
Very informative. Thank you sir and hope you get well soon 😊😊😊!!!!
Thank you Aashish.
Brilliant expression of the topic 😀
Thank you Karthika.
very good explanation sir..
Thanks Sreya.
Nizz explanation and its interesting
Thanks Libinchetta.
So keenly explained in spite of the handicap. Get well soon Sir.
Thanks Rebeca.
Very nice explanation sir….Iam really feel proud that Iam your student!!!
Thank you so much Jeeva.
Very Good.
What happened Sir? Really something happened?
Thank you Parvathy. Yes really happened.
Great Sir. I salute you👏🏻👏🏻👏🏻
Thank you Rajeev.
Again a GREAT explanation……
Keep going on Sir…
GOD has a reason for allowing things to happen.We may never understand his wisdom , but we simply have to trust his will.
Sometimes bad things happen for a Good reason. 🙂 GET WELL SOON Sir.
Broken Crayons Still Colour. 🙂
Thank you so much Sapna.
I love this : “Broken Crayons Still Colour.”
Very good explanation sir
The way you explaining the topics will always on the mind. Happy to get such kind of information in different topics.
Thank you sir🤗
its really attractive way of teaching which helps us to remind the keywords easily. thank you so much sir
Very informative… Thank you very much sir….
Thank you sir,very informative Blog& also well explained ☺️👍
Thank you sir. Very well explained.
Good explanation..Thank you so much sir
Excellent explanation, thank you so much
Well Explained. Thank you sir.
Nicely explained Sir…👍👌