4
Answers

Final , FInally And Finalize

Photo of Ashwani Tyagi

Ashwani Tyagi

12y
36.5k
1
Hello Friends,

                     I want to know the difference between Final,Finally and Finalize method?

Answers (4)

1
Photo of Vinitha T
NA 485 54 3y

In Java,

Final - Access modifier and keyword, which is used to have restrictions on Class, Method or Variables. Final variable cannot be modified, it becomes constant. Final method cannot be overridden. Final class cannot be extended.

Code:

public class Demo{

     final int value= 20;

     void display(){

         value=10;

     }

     public static void main(String[] args){

     Demo d=new Demo();

     d.display();

     }

 } 


Error: can't assign a value to final variable value 


Finally -  Block in Exception handling used to execute the code in block whether exception occurs or not.


Code:

public class Demo{ 

    public static void main(String[] args){

    try{

      System.out.println("Try block");

      int value = 5/0;

      System.out.println(value);

    } 

    catch (ArithmeticException ex){

    System.out.println("Exception");

    System.out.println(ex);

    } 

    finally{

    System.out.println("Finally block"); 

    } 

    } 

 } 


Output : Try block

         Exception

         Exception as / by zero

         Finally block. 

Finalize -  Method of object class used to perform clean up process before object is collected by Garbage. It is executed before the object is destroyed.

Code:

  public class Demo{ 

     public static void main(String[] args){

        Demo d = new Demo();

        d = null;

        System.gc();

        System.out.println("Grabage collection");

      }

      protected void finalize(){

        System.out.println("Finalize()");

      }

  }        

Output: Garbage collection

        Finalize() 

0
Photo of mayur kohli
NA 6 2 7y
final is a keyword used to declare constants.
finally block is used in an exception handling.whether there is an exception or not in try block in , finally block will be executed.we used to provide the code like like deallocating resource finalize method will be called by garbage collector to remove the object from memory if it no longer has been used.we can call it explicitely ,but we are not sure that it will remove an object.
Read More :- http://crbtech.in/Java-Training/distinction-final-finally-finalize-method/
0
Photo of Jignesh Trivedi
NA 62.3k 46.3m 12y
hi,

final – constant declaration.
finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs.
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.

hope this will help you.
0
Photo of Satyapriya Nayak
NA 39.3k 13.3m 12y
http://javarevisited.blogspot.in/2012/11/difference-between-final-finally-and-finalize-java.html

http://ecomputernotes.com/java/control-structures-in-java/difference-between-final-finally-and-finalize

http://www.brilliantsheep.com/difference-between-final-finally-and-finalize/