Login with  Log in with facebook
Hiring Manager? SIGN UP HERE

what is the difference between final, finally and finalize.

Rishi Yadav
08/05/2011 at 20:50
3 Answers

final = It is a keyword used to stop the subclassing. It is also used to define constant variables whose value cannot be changed further in the code. Method declared as final cannot be overridden

finally = this is the part of JAVA Exception handling written along with the try catch block. finally block always get executed even if the error arises in catch block, runtime system makes sure that finally block is executed.

finalize = This is the method used to force the garbage collection.

Poonam Sutaria
08/06/2011 at 17:57


Only some precisions...

final : ... to define constant references to variables.....Have a look to immutable classes to understand that a private final member can be modified from outside of the object.

finally : always get executed unless a System.exit() is encountered.

finalize : garbage collector cannot be forced. This method is always called before garbage collection of an object.

Franck Gaille
08/09/2011 at 12:32


Things that happen within the finally block are guaranteed to occur no matter what happens in the try-catch-block. If an exception happens that is not encapsulated by Exception (e.g., extends Throwable, such as various Errors), then it still runs the finally block.

One thing to be aware of: if, within the finally block, a RuntimeException is thrown, or another Exception escapes from within it, then the rest of the finally block will not execute. Also, as Lord Torgamus pointed out, it is contingent on the JVM running. In addition, and probably obviously, it is also contingent on the thread not being stopped.

Maneshwar Meel
09/14/2011 at 22:01

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *