final
final (lowercase) is a reserved keyword in java. We can’t use it as an identifier, as it is reserved. We can use this keyword with variables, methods, and also with classes.
final with Variables: The value of the variable cannot be changed once initialized. If we declare any variable as final, we can’t modify its contents since it is final, and if we modify it then we get Compile Time Error.
final with Class: The class cannot be subclassed. Whenever we declare any class as final, it means that we can’t extend that class or that class can’t be extended, or we can’t make a subclass of that class.
final with Method: The method cannot be overridden by a subclass. Whenever we declare any method as final, then it means that we can’t override that method.
finally
The finally keyword is used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown. The final block will be executed after the try and catch blocks, but before control transfers back to its origin. finally is executed even if try block has return statement.
class Test{
static void A()
{
try {
System.out.println("inside A");
throw new RuntimeException("demo");
}
finally
{
System.out.println("A's finally");
}
}
public static void main(String args[])
{
try {
A();
}
catch (Exception e) {
System.out.println("Exception caught");
}
}
}
Output:
inside A
A's finally
Exception caught
Finalize method
It is a method that the Garbage Collector always calls just before the deletion/destroying of the object which is eligible for Garbage Collection, so as to perform clean-up activity.
reference
https://www.geeksforgeeks.org/g-fact-24-finalfinally-and-finalize-in-java/