1.

Final Vs Finally Vs Finalize?

Answer»
  • final - final keyword is used to restrict in some way. It can be used with VARIABLES, methods and classes. When a variable is declared as final, its VALUE can not be changed once it is initialized. Except in case of blank final variable, which must be initialized in the constructor.

    If you make a method final in Java, that method can't be overridden in a sub class.

    If a class is declared as final then it can not be sub classed.
  • finally - finally is part of EXCEPTION handling mechanism in Java. finally block is used with try-catch block. finally block is always executed whether any exception is thrown or not and raised exception is handled in catch block or not. Since finally block always executes thus it is PRIMARILY used to close the opened resources like database connection, file handles etc.
  • finalize() - finalize() method is a protected method of java.lang.Object class. Since it is in Object class thus it is inherited by every class. This method is called by garbage collector thread before removing an object from the MEMORY. This method can be overridden by a class to provide any cleanup operation and gives object final chance to cleanup before getting garbage collected.
  • protected void finalize() throws Throwable
  • {
  • //resource clean up operations
  • }



Discussion

No Comment Found