File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

difference between finally and finalize()

rajender polu
Greenhorn

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

Joined: May 26, 2003
Posts: 30116
    
150

Rajender,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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 ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
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 Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
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,
try{
}catch(Exception e){
}finally{
//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: http://aspose.com/file-tools
 
subject: difference between finally and finalize()
 
Similar Threads
finally , finalize()
Thread
tips for C++ programmers
Difference b/n finally and finalize
concept about finalize