Tuesday, 3 June 2014

Difference between final,finalize and finally

1) final keyword:-

          The final keyword is used to restrict the user. This variable can be applied to variable,method and class.
          If you declare the variable as final, you can not change the value of the final variable.(i.e reassignment is not possible for final variables)
         Example ,
                     final String name="anil";
                     name="mahesh";   //it will give compile time error.

        If you declare the method as final then you can not override the method in the sub class. Overriden is not possible for the final methods.
                        class Employee{
                               public final String details(){
                                                 return "details";

                          class Manager extends Employee{
                                public String details(){                                   //will give error
                                       return "details manager";
        If you declare class as final then you can not extend that class means inheritance is not possible for final classes.
                   final  class Employee{
                              //some statements
                   class Manager extends Employee{              //compilation error,cannot inherit
                              //some statements                           // from final class

2) finally:

                  finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurred or not.(Except calling System.exit(1)).
                                                      //some statements
                                     catch(Exception e){
                                                     //exception handling block
                                                     //e.g close database connection interface,
                                                     //e.g close file resources & so on.....

  3) Finalize() method:-

       Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected.

    Your class can provide for its finalization simply by defining and implementing a method in your class named finalize(). Your finalize() method must be declared as follows:

        protected void finalize () throws throwable

    This class opens a file when its constructed:

        class OpenFile {
              FileInputStream aFile = null;
              OpenFile(String filename) {
                   try {
                         aFile = new FileInputStream(filename);
                    } catch (java.io.FileNotFoundException e) {
                          System.err.println("Could not open file " + filename);

    To be well-behaved, the OpenFile class should close the file when its finalized. Here's the finalize() method for the OpenFile class:

        protected void finalize () throws throwable {
              if (aFile != null) {
                    aFile = null;

    The finalize() method is declared in the java.lang.Object class. Thus when you write a finalize() method for your class you are overriding the one in your superclass.

Why not use Finalize() method :-

      There are some reasons not to use finalize() metod are as follows:

1) finalize() execution is not guaranteed at all.

2) finalize() methods do not work in chaining like constructors. It means like when you call a constructor then constructors of all super classes will be invokes implicitly. But, in case of finalize methods, this is not followed. Super class’s finalize() should be called explicitly.

3) Any Exception thrown by finalize method is ignored by GC thread and it will not be propagated further, in fact it will not be logged in your log files.

4) finalize() add heavy penalty in performance.

 Related Post:--
1) Exception Handling Interview questions and answers

No comments:

Post a Comment