What is the difference between finally block and finalize method?

Questions by challanaveen

Showing Answers 1 - 3 of 3 Answers

anuragverma

  • Nov 10th, 2006
 

finally -The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block (except System.exit(0) call). Use the finally block to close files or to release other system resources like database connections, statements etc.finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used 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 non-memory resources through the finalize() method.

  Was this answer useful?  Yes

srinivasreddy

  • Nov 20th, 2006
 

  A method which is declared as finalize can not be overridden in subclasses which extends the class has the finalized method

 Finally block will be exeuted in a programe at any case

if wether programe terminates in try or catch block finally it executes the finnaly block.

  Was this answer useful?  Yes

ujwala

  • Dec 15th, 2006
 

finalize() method is executed prior to the execution of a garbage collector. When a object is no longer in use, but it holds some resources, before garbage collector collects this object these resources must be released. you should define finalize() method so that before the gc takes places all the resources released.While finally block is optional and executed before the class exicution exits. Finally block is used to release database connections , system resources. Execution of finally block do not depend upto the execution of try catch block.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions