File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize help plzzz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize help plzzz" Watch "finalize help plzzz" New topic

finalize help plzzz

ArnabKarmakar karmakar

Joined: Oct 04, 2004
Posts: 5
I have written a small code. When the program is over, the finalize function should be called. But it is not calling that one. You can have a look in my code.

According to me it should print "The object is going to be deleted" three times. But it is not printing.
Please help me out.
Thankz in advance.


(You too can use [C0DE][/C0DE] tags )
[ October 09, 2004: Message edited by: Barry Gaunt ]
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
I do not believe that the garbage collection will run automatically (and hence not your finalize method). If may not run even if you ask it to do so (kind of like trying to teach a cat to sit). I was able to get the output you are looking for with the following slightly modified code:

Note that although I have asked for the garbage collection to execute, it may not. At the very least, you have to provide some objects to be available for garbage collection before the application is finished which is what I have done by setting the object references to null.

Does this help?
ArnabKarmakar karmakar

Joined: Oct 04, 2004
Posts: 5
thank you,
its working.
But can we always run the gc successfully whenevr we want.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can call System.gc() whenever you wish, but you cannot be sure that all eligible objects will be garbage collected, if any.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: finalize help plzzz
It's not a secret anymore!