0 votes
in JAVA by
Do final, finally and finalize keywords have the same function?

1 Answer

0 votes
by
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;  // error

The 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

}

Related questions

0 votes
asked Oct 18, 2019 in C Sharp by Robin
0 votes
asked May 3, 2021 in JAVA by Robindeniel
...