Forums Register Login
Difference b/n finally and finalize

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.

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.

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


Originally posted by Campbell Ritchie:

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

do ! try this while int public
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.

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?
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 1823 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 02:47:23.