Showing posts with label finally and finlize() method in Java. Show all posts
Showing posts with label finally and finlize() method in Java. Show all posts

Difference between final, finally and finlize() method

Final and finally is keywords where as finalize() is a method


Finalize():

Finalize() is a method, used to call garbage collector to reclaim the memory.

Finally:
Irrespective of the exception raised, finally block will be executed.

Final:

In java, final is a keyword used to define constants.
Final keyword can be applied in 3 levels mainly.
1      Class level
2               Method level
3           Variable level

Class level:

Final class:

The class declared as final can't be subclass or extend the class

The final class declared as follows :
public final class MyFinalClass

Method level:

     final method:

          when we declare a method as final, Then final method can't be override in a subclass.
         
          The final method  can be declare as follows:

           public final String convertCurrency()

     Variable level

     Final variables:

      When we declare a variable as final, it behaves like a constant.
      Means once it is declared, it can't be changed.
      Attempt to change  it's value lead to exception or compile time error.
    
      You can declare the final fields as :

      public final double radius = 126.45;