Thursday, October 16, 2008

Final and Finally

Final
  • Final is a keyword which can be placed in front of a method or variable or a class.
  • If final is used in front of a variable then the value of it will not get changed.
  • If the final keyword is placed in front of a method then that method cannot be overridded or overloaded also.
  • If final is placed in front of a class then the class cannot be instantiated.

Finally

  • Finally is a block of code written after the try catch blocks.
  • This block is executed even if an exception has occurred and not handled.
  • In this block we can write the code which should be compulsorily executed like closing the connection object even if exception occur.
  • There can be only 1 finally block per try block and it must be the last block after all catch blocks if present.
  • In finally, you cannot use GoTo or Return Statements.

No comments: