Untitled Document

Subscribe here to get updates on new Videos

 

1. Difference between final, finally, finalize?

 

 

 

homenext

 

final:

1. Final is a modifier applicable for classes methods and variables. If a class declared as final then we can't extend that class. i.e we can't create child class for that class.
2. If a method declared as final then we can't override that method in the child class.
3. If a varaiable declared as final then it will become constant and we can't perform re-assignment for that variable.

finally:

  1. finally is a block always associated with try catch to maintain cleanup code.

try
{
   // risky code…
}
catch( X e)
{
                Handling code
}
finally
{
//cleanup code
                }

finalize():

  1. finalize() is a method which is always invoked by garbage collector just before destroying an object to perform cleanup activities.

Note:

  1. finally meant for cleanup activities related to try block. where as finalize() meant for cleanup activities related to object.