Java - final, finally, and finalize

final keyword
The Final is used to apply restrictions on class, method, and variable. The final class can't be inherited, the final method can't be overridden and the final variable value can't be changed.Final is a keyword.
final with Variables: The value of a variable cannot be changed once initialized.

class KnowledgeFactory {
public static void main(String[] args) {
// Non final variable
int salary = 100;

// final variable
final int age = 25;

// modifying the non final variable : Allowed
salary++;

// modifying the final variable :
// Immediately gives Compile Time error.
age++;
}
}
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 class KnowledgeFactory {

public void myMethod() {
// TODO
}
}

// here gets Compile time error that
// we can't extend KnowledgeFactory as it is final.
class Knf extends KnowledgeFactory {
// TODO
}
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.

class Knf {
final void myMethod() {
}
}

class KnfChild extends Knf {

// Here we get compile-time error
// since can't extend rr since it is final.
@Override
void myMethod() {
}
}

finally keyword(block)
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 finally block will be executed after the try and catch blocks, but before control transfers back to its origin.Finally is a block.

public class Knowledgefactory {

public static void main(String[] args) {

try {
int i = 2 / 0;
} catch (ArithmeticException e) {
System.out.println(e);
} finally {
System.out.println("Inside finally block,Executes whether exception occurs or not");
}
try {
int i = 4 / 2;
} catch (ArithmeticException e) {
System.out.println(e);
} finally {
System.out.println("Inside finally block,Executes whether exception occurs or not");
}
}
}
Output:
java.lang.ArithmeticException: / by zero
Inside finally block
Inside finally block
finalize method
It is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection or we can say resource de-allocation. Remember it is not a reserved keyword.
Once the finalize method completes immediately Garbage Collector destroys that object. finalize method is present in the Object class

public class Knowledgefactory {

public void finalize() {
System.out.println("finalize called");
}

public static void main(String[] args) {
Knowledgefactory knfobj1 = new Knowledgefactory();
Knowledgefactory knfobj2 = new Knowledgefactory();
knfobj1 = null;
knfobj2 = null;
System.gc();
System.out.println("Main completed");
}
}
Output:
Main completed
finalize called
finalize called

by sibin
web solution consultant for the startups
+91-8075866854

Comments