File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes difference between finally and finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "difference between finally and finalize()" Watch "difference between finally and finalize()" New topic

difference between finally and finalize()

rajender polu

Joined: Sep 07, 2008
Posts: 2
what is the difference between finally block and finalize method? where finalize is useful?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Welcome to JavaRanch!

The finally block executes after a section of code in a method. For example, if you have code that might throw an exception, the finally block executes whether the exception is thrown or not. It is guaranteed to run.

The finalize method is supposed to be called before an object is garbage collected. It is not guaranteed to be called in that an object might not be garbage collected.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

When an object is to be garbage collected finalize() method will be called by the JVM only once. If we passed an reference to the object within finalize() (of course we should have override the finalize() within that class) then the object will not be garbage collected. But next time when the JVM decided to remove the object from the heap, finalize will not be called in that object since JVM knows it called the finalize once before in that same object.
[ September 07, 2008: Message edited by: Vijitha Kumara ]

[How to ask questions] [Twitter]
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3371
as mentioned before you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method. Finally is more useful.

[] | [Java job hunting know how] | [Java resumes]
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
Originally posted by rajender polu:
what is the difference between finally block and finalize method? where finalize is useful?

finally block is used in Exception Handling.
When user wants his program to perform some action even though some exception occurs during program then he or she can write that code in finally block.
which is like,
}catch(Exception e){
//Code goes here

Where finalize() method is used for garbage collection when object is ready for garbage collection then Automatic Garbage collector will call finalize() method before destroying that object.
I agree. Here's the link:
subject: difference between finally and finalize()
It's not a secret anymore!