Pages

difference between final, finally, finalize

Q) What is the difference between final, finally, finalize?
Final: - When we declare a sub class a final the compiler will give error as “cannot subclass final class” Final to prevent inheritance and method overriding. Once to declare a variable as final it cannot occupy memory per instance basis.
  
     Final class cannot have static methods       
     Final class cannot have abstract methods
     Final class can have only a final method.
Finally: - Finally create a block of code that will be executed after try catch block has completed. Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.                       
    Using System.exit (); in try block will not allow finally code to execute

Finalize: - sometimes an object need to perform some actions when it is going to destroy, if an object holding some non-java resource such as file handle (or) window character font, these resources are freed before the object is going to destroy any cleanup processing before the object is garbage collected.