final keyword

The final keyword in Java is used to restrict the user. final can be used for

  1. Variable
  2. Method
  3. Class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.

Java final variable cannot change the value of final variable (It will be constant). A final method cannot be overridden and a final class cannot be extended.

public class Parent {
  int field1 = 1;
  // Final variable
  final int field2 = 2;

  Parent() {
      field1 = 2; // OK
      field2 = 3; // Compilation error

  // Method with 'final' argument
  void method1(int arg1, final int arg2) {
    arg1 = 2; // OK
    arg2 = 3; // Compilation error
  // Final method with 'final' local variable
  final void method2() {
    final int localVar = 2; // OK
    localVar = 3; // Compilation error

// Final subclass class
public class final Child extends Parent {
  void method1(int arg1, int arg2) {
      // OK
  final void method2() {
      // Compilation error. Overriding of 'final' method is not allowed

// The Child class is final and therefore impossible to extend.
public class GrandChild extends Child {
    // Compilation error


finally keyword

The finally block is an optional block to use with a try/catch statement. In this block, we include code to execute after the try/catch structure, whether an exception is thrown or not. It’s even possible to use it with the try block without any catch block provided we include a finally block. The code will then be executed after the try or after an exception is thrown.

// Java program to illustrate use of finally block 
class ExceptionHandling { 
  public static void main(String[] args) 
    PrintWriter out = null; 
    try { 
      System.out.println("Entered try statement"); 

      // PrintWriter classes in io package 
      out = new PrintWriter(new FileWriter("OutFile.txt")); 
    catch (IOException e) { 
      // FileWrite can throw IOException 

    // Following finally block cleans up and then closes the PrintWriter. 
      if (out != null) { 
        System.out.println("Closing PrintWriter"); 
      } else { 
        System.out.println("PrintWriter not open"); 

Important points regarding finally block

  • In normal case when there is no exception in try block then the finally block is executed after try block. However if an exception occurs then the catch block is executed before finally block.
  • An exception in the finally block, behaves exactly like any other exception.
  • The statements present in the finally block execute even if the try block contains control transfer statements like return, break or continue.
  • System.exit() statement behaves differently than return statement. Unlike return statement whenever System.exit() gets called in try block then finally block doesn’t execute if there are no exception. However if any exception occurs while calling System.exit(0) then finally block will be executed.


The circumstances that prevent execution of the code in a finally block are:

  • The death of a Thread
  • Using of the System. exit() method.
  • Due to an exception arising in the finally block.


finalize Method

And finally, the finalize method is a protected method, defined in the Object class. It’s called by the garbage collector on objects that aren’t referenced anymore and have been selected for garbage collection. Like any other non-final method we can override this method to define the behavior an object must have when collected by the garbage collector.

Once finalize method completes immediately Garbage Collector destroy that object. finalize method is present in Object class and its syntax is:

protected void finalize throws Throwable{}


Since Object class contains finalize method hence finalize method is available for every java class since Object is superclass of all java classes. finalize method which is present in Object class, has empty implementation, we have to override this method to define our own clean-up activities.

class FinalizeObject
  protected void finalize() throws Throwable {
    System.out.println("Execute finalize method");

public class Test
  public static void main(String[] args)
    FinalizeObject object = new FinalizeObject();
    object = null;

In this example, we override finalize() method in our object and create a main() method which instantiates our object and immediately drops the reference by setting the created variable to null.

After that, we call System.gc() to run the garbage collector (at least we expect it to run) and wait for a second (just to ensure that the JVM doesn’t shut down before garbage collector has the chance to call finalize() method).