Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between final,finally and finalize in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 296
    Comment on it

    1) Final:

    Final is a keyword. It is used to store constant values in variable. The value can't be changed later on.
    The class which is declared as final cannot be inherited.
    The method which is declared as final cannot be overridden.

    Example:

    class Demo{  
    public static void main(String[] args){  
    final int x=50;  
    x=100;//Compile Time Error  
    }}  
    

    2) Finally:

    Finally is generally a block of statements. This block is always executed, therefore it can be used to insert some important code that requires to be executed every time.
    The code inside the finally block is executed even if any exception also occurs and it is handled or not.

    Example:

    class Demo{  
    public static void main(String[] args){  
    try{  
    int x=100;  
    }catch(Exception exception){
     System.out.println(exception);
    }  
    finally{System.out.println("This is finally block");}  
    }}  
    

    3) finalize()

    This is method and it is used to release any resources that is held by the object.
    This method is executed before the object is garbage collected, so you can do the cleanup tasks for the objects using this method.

    Example:

    class Demo{  
    public void finalize()
     {System.out.println("inside finalize");}  
     public static void main(String[] args){  
      Demo d1=new Demo();  
      Demo d2=new Demo();  
      d1=null;  
      d2=null;  
      System.gc();  
    }}  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: