InterviewSolution
| 1. |
Do final, finally and finalize keywords have the same function? |
|
Answer» All three keywords have their own utility while programming. Final: If any restriction is required for classes, variables, or methods, the final KEYWORD comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example: final int a=100;a = 0; // errorThe second statement will throw an error. Finally: It is the block present in a program where all the codes WRITTEN inside it get executed irrespective of handling of exceptions. Example: try {int variable = 5;}catch (Exception exception) {System.out.println("Exception occurred");}finally {System.out.println("Execution of finally block");}Finalize: Prior to the GARBAGE collection of an object, the finalize method is called so that the clean-up activity is implemented. Example: public static void main(String[] args) {String example = new String("InterviewBit");example = NULL;System.gc(); // Garbage COLLECTOR called}public void finalize() {// Finalize called} |
|