Difference between Finally Block and Finalize()?

Hi Guys,

This is Ravi, I would like to know the exactdifference between Finally block and Finalize().Which should be used when? I Know that it is possible to close file pointers, connections etc. in both the cases?

Note: Pleasedont tell me finally comes after try catch and finalize before Object Garbage Collection. Tell me if there is any other valid reason or example that can be used to understand the concept.

Thanks,

Ravi.

[484 byte] By [yrnchowdarya] at [2007-11-27 3:49:00]
# 1
The finally block is a language construct and the finalize() method is a deprecated method that shouldn't be used since there's no guarantee when, if at all, it will be called.
-Kayaman-a at 2007-7-12 8:52:50 > top of Java-index,Java Essentials,Java Programming...
# 2

> Note: Please dont tell me finally comes after try

> catch and finalize before Object Garbage

> Collection. Tell me if there is any other valid

> reason or example that can be used to understand the

> concept.

Why not? they have nothing to do with each other, so it's pretty difficult to explain the difference. What's the difference between an apple and a car? And apple is natural while a car has more power?

CeciNEstPasUnProgrammeura at 2007-7-12 8:52:50 > top of Java-index,Java Essentials,Java Programming...
# 3

> ... what is the finally block

Statements in the finally block are executed when the thread leaves the associated try-catch block

[unless you catch and fail to re-throw ThreadDeath - so don't].

> ... what is finalize()

The finalize() method is executed before the object is garbage collected

[if finalize() was called earlier and you revived the object - finalize() will not be called again before the object is garbage collected].

Note that objects might never be garbage collected before the JVM exits - in which case finalize() might never be called on those objects.

> Which should be used when?

Put some log statements in and see what suits your application best.

> Please dont tell me finally comes after try

> catch and finalize before Object Garbage Collection.

Why not? It is a reasonably accurate account.

> Tell me if there is any other valid reason

> or example that can be used to understand the concept.

We don't know what it is about the concept you find difficult to understand...

tschodta at 2007-7-12 8:52:50 > top of Java-index,Java Essentials,Java Programming...