Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference b/n finally and finalize

 
khurram faraaz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:

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


do ! try this while int public
 
khurram faraaz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic