Difference b/w final, finally & finalize in java (with examples)

Let us discuss final, finally and finalize in java with code examples.

What is final ?

  • final is a keyword in java.
  • final keyword can be applied to followings:
    • Class
    • Method(s)
    • Variable(s)

final class:

  • When final keyword  is used with any class, then class becomes final class.
  • The final class cannot be overridden by concrete class.
  • If we extend final class, compiler will report an error.
    • We will get the compilation error saying “cannot subclass the final class”.

Program to demonstrate final class in java.


final class Vehicle {
 private int doors;
}

class NewVehicle extends Vehicle {
 // Cannot subclass final class
}

What is final method?

  • When final keyword used with any method of class,  then method becomes final method.
  • The final method cannot be overridden by a method in concrete class. We will get the compilation error,

Program demonstrating final method of Person class.

class Person {
 public final void setSalary() {

 }
}

class Manager extends Person {
 public void setSalary() {
  // cannot override final method of Person class
 }
}

What is final variable?

  • When final keyword used with any variable of class
    • then variable value cannot changed after being initialized.
  • We will get the compilation error, if we try to assign new value to final variable(s).
final int salary = 100;
//Compilation error: final variable cannot be assigned
salary = 200;

finalize:

  • finalize is a method of object class.
    • protected void finalize()
  • finalize method is used to clean up the resources.
  • finalize method can be overridden by concrete class.
    • The garbage collector may call the finalize method before destroying the current object.
    • Its is not confirmed, when the finalize method will be called by garbage collector

Program to demonstrate finalize method

class TestFinalize {
 public static void main(String[] args) {
  TestFinalize obj = new TestFinalize();
                System.out.println("Executing main method");
  obj = null;
  // Signal the GC to collect the garbage
  System.gc();
 }

 protected void finalize() {
  System.out.println("Executing finalize method");
 }
}

Output of finalize method:

Executing main method
Executing finalize method

finally:

  • finally is a code block, used with try and/or catch block.
  • finally block always executes irrespective of exception occurred or not.
  • As finally block always gets executed, irrespective of exceptions conditions,
    • The clean up code is generally kept in finally block.

Program to demonstrate finally block in java

class Student {
 public static void main(String[] arg) {

  try {
   int salary = Integer.valueOf("NumberFormatException");
  } catch (Exception exp) {
   System.out.println("Executing exception block");
  } finally {
   System.out.println("Executing finally block");
  }
 }
}

Output of finally block:

Executing exception block
Executing finally block
Scroll to Top