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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 298
    Comment on it

    final, finally and finalize in Java

    final:

    The meaning of final keyword varies with it's usage with class,methods and variables.

    Class: When final keyword is placed before class keyword then that class cannot be inherited i.e final class cannot be subclassed.

    Methods: Using final keyword with methods causes methods not to be overridden by subclass.

    Variables: A variable declared as final cannot change it's value once initialized.

    finally:

    The finally keyword is associated with try/catch block of exception. The finally block is executed whether or not exception is thrown. A try block in exception either has catch block or finally block or both.

    A Java program to demonstrate finally.

    class Demo
    {
        // A method that throws an exception and has finally. 
        // This method will be called inside try-catch in main.
        static void  First()
        {
            try
            {
                System.out.println("inside First");
                throw new RuntimeException("demo");
            }
            finally
            {
                System.out.println("First finally");
            }
        }
    
        // This method also calls finally. This method
        // will be called outside try-catch in main.
        static void  Second()
        {
            try
            {
                System.out.println("inside Second");
                return;
            }
            finally
            {
                System.out.println("Second finally");
            }
        }
    
        public static void main(String args[])
        {
            try
            {
                First();
            }
            catch (Exception e)
            {
                System.out.println("Exception caught");
            }
            Second();
        }
    
        } 
    

    Output of above program:

    inside First
    First finally
    Exception caught
    inside Second
    Second finally

    finalize:

    The method finalize() is called by automatic garbage collector just before destroying objects i.e this method does the clean up work like closing open files,releasing resources that were taken while running programs etc. This method is in the base object class can be overridden to define cutom behaviour.

    Syntax of finalize method:

    protected void finalize() throws Throwable
    {
        /* close open files, release resources, etc */
    }
    

 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: