aspose file tools*
The moose likes Beginning Java and the fly likes Difference b/n finally and finalize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference b/n finally and finalize" Watch "Difference b/n finally and finalize" New topic
Author

Difference b/n finally and finalize

khurram faraaz
Greenhorn

Joined: May 01, 2006
Posts: 2
Hello

I am new to this forum and to java, pls let me know what is the difference b/n finally { } and finalize

Why do we need the two.

Regards
khurram
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
I have no idea about finalize. But I can tell you about finally.
finally is a block that we place after a try/catch block. It is used for clean-up operations such as closing of a socket, or a file.
eg.


From the above code its clear that we have duplication of code. We are placing one set of code in the try block and the other in the catch block for closing the streams.

This is where the finally block comes in. We place those types of codes here, that need to be executed, irrespective of whether an exception gets thrown or not.



You might argue that the finally blocks are still useless, because we could have had closed the streams after the try/catch block.

Well, finally blocks have special features, such that if you place a return statement in the try/catch block and you have a finally block that needs to be executed, then the method will return ONLY AFTER it executeds the finally block.

I know I haven't been clear about the special usage of finally but I could't get a good example into my head at the moment.
Sorry for that.


With Best Regards,
Shyam Prasad Murarka
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You ahve had finally: now finalize().

It is a method which is found in the java.lang.Object class; you can find a description in the API specifications. Because it is in the Object class, it is implicitly found in every object you create in every application you program.

Whenever there are no more references to this object, the garbage collector will delete the references to it, and when it does so it calls the finalize() method.

You will hardly ever need to use finalize() at all. Just remember it is there and leave it alone to get on with its work in the background.

You haven't asked the difference between finally and final.

CR
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Campbell Ritchie:

You haven't asked the difference between finally and final.


do ! try this while int public


There is no emoticon for what I am feeling!
khurram faraaz
Greenhorn

Joined: May 01, 2006
Posts: 2
Thanks for the answers.

I understood the usage of finally { } and finalize.

we use finally for clean up activities(to free resources like stream objects, to close sockets etc)
we use finalize() for garbage collection

they perform similar activities if i am not wrong, in that case why should we have two different concepts of finally and finalize, pls this is a very humble qs, I want to know if there is really any difference b/n the two.

Regards
khurram
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

After these explanations, I can't see how you think they're the same.

"finally" is used to clean up after a block of code

"finalize()" is a method that can participate in disposing of an object

"finally" is used very often, while "finalize()" is rarely used and of little practical use.

By the way, what does Barnes and Noble (b/n) have to do with this question?


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference b/n finally and finalize