Showing posts with label finally EXCEPTIONS. Show all posts
Showing posts with label finally EXCEPTIONS. Show all posts

Thursday 14 January 2016

Difference between final, finally and finalize in java


final -
final is a keyword.
When a variable declared as final should be initialized only once and it cannot be changed.
In case of classes, final classes cannot be extended
In case of methods, final methods cannot be overridden.


finally -
finally is a block
finally is used in exception handling, is used to place code that are important and it will be executed whether the exception is handled or not.The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.


finalize -
finalize is a method.
Before an object is garbage collected, the run time system calls its finalize() method. That means this method used for clean up processing before object is garbage collected

Thursday 23 January 2014

BASIC finally EXCEPTION HANDLING IN JAVA

Exceptions:The finally Block 

Example to show Finally exception in java

The Finally block in java is used along with the try-catch statements.This block executed even after the unexpected exception occured.The Run time always execute the expression in finally block irrespective of the try block.  The main usage of finally block is to do clean up job. Keeping cleanup code in a finally block is always a good practice, even when no exceptions are occured. The runtime system always executes the code within the finally block regardless of what happens in the try block. So it is the ideal place to keep cleanup code.