DEV Community

Cover image for ☕Understanding `final`, `finally`, and `finalize` in Java
Mark Yu
Mark Yu

Posted on

☕Understanding `final`, `finally`, and `finalize` in Java

Java programming involves a myriad of keywords, each serving distinct purposes to enhance the functionality and robustness of the code. Among these, final, finally, and finalize often cause confusion due to their similar nomenclature. However, they serve entirely different purposes. This article will elucidate the differences between these keywords, their uses, and practical examples to clarify their roles in Java programming.

Image description

Introduction

In Java, final, finally, and finalize are keywords with distinct functions:

  • final: A keyword used in variable, method, and class declarations to denote constants, prevent method overriding, and inheritance.
  • finally: A block used in exception handling to execute code regardless of whether an exception is thrown or not.
  • finalize: A method used to perform cleanup operations before an object is garbage collected.

Understanding the differences and applications of these keywords is crucial for writing effective Java code.

The final Keyword

Purpose

The final keyword is versatile and can be applied to variables, methods, and classes.

Use Cases

  1. Final Variables: When applied to a variable, the final keyword makes it a constant, meaning its value cannot be changed once assigned.
  2. Final Methods: When applied to a method, it prevents the method from being overridden by subclasses.
  3. Final Classes: When applied to a class, it prevents the class from being subclassed.

Code Examples

Final Variables



public class Constants {
    public static final int MAX_USERS = 100;
    public static final String APP_NAME = "MyApp";
}


Enter fullscreen mode Exit fullscreen mode

In this example, MAX_USERS and APP_NAME are constants that cannot be changed.

Final Methods



public class Parent {
    public final void display() {
        System.out.println("This is a final method.");
    }
}

public class Child extends Parent {
    // This will cause a compile-time error
    // public void display() {
    //    System.out.println("Attempting to override a final method.");
    // }
}


Enter fullscreen mode Exit fullscreen mode

Here, the display method in the Parent class cannot be overridden by the Child class.

Final Classes



public final class Utility {
    public static void performTask() {
        System.out.println("Performing a task.");
    }
}

// This will cause a compile-time error
// public class AdvancedUtility extends Utility {
// }


Enter fullscreen mode Exit fullscreen mode

The Utility class cannot be subclassed due to the final keyword.

The finally Block

Purpose

The finally block is used in exception handling to execute code that must run regardless of whether an exception is thrown or caught.

Use Cases

  1. Resource Management: Ensuring resources like files and database connections are closed properly.
  2. Cleanup Operations: Performing necessary cleanup actions after try-catch blocks.

Code Example



public class FileOperations {
    public void readFile(String filePath) {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(filePath);
            // Perform file operations
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    System.out.println("Failed to close the file: " + e.getMessage());
                }
            }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

In this example, the finally block ensures that the FileReader resource is closed regardless of whether an exception occurs.

The finalize Method

Purpose

The finalize method is invoked by the garbage collector before an object is reclaimed. It is used to perform cleanup operations, such as releasing resources.

Use Cases

  1. Cleanup Operations: Performing cleanup before an object is garbage collected.

Code Example



public class ResourceHolder {
    private FileReader fileReader;

    public ResourceHolder(String filePath) throws FileNotFoundException {
        this.fileReader = new FileReader(filePath);
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (fileReader != null) {
                fileReader.close();
            }
        } finally {
            super.finalize();
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Here, the finalize method ensures that the FileReader is closed before the ResourceHolder object is garbage collected. However, it's important to note that the finalize method is deprecated in recent Java versions due to unpredictability and better alternatives like try-with-resources and explicit resource management.

Summary

In summary, final, finally, and finalize are three distinct keywords in Java with different purposes:

  • final: Used to declare constants, prevent method overriding, and inheritance.
  • finally: A block used in exception handling to execute necessary code regardless of exceptions.
  • finalize: A method used for cleanup operations before an object is garbage collected (now largely deprecated).

Understanding these keywords helps in writing more robust, maintainable, and efficient Java code. By using final, you can create immutable variables and secure methods and classes. The finally block ensures resource management and cleanup, while finalize (despite its deprecation) shows the historical approach to object cleanup before garbage collection.


References:

Top comments (3)

Collapse
 
prsaya profile image
Prasad Saya

Consider the following Java program:

public class Main1 {
    final int i;
    public static void main(String[] args) {
        new Main1();
    }
    Main1() {
        i = 10;
        System.out.println("Value of i is " + i);
    }
}
Enter fullscreen mode Exit fullscreen mode

Will this compile? Runs without errors? Does this print "Value of i is 10"?

More about final variables at final Variables (Java Language Specification)

Collapse
 
aminmansuri profile image
hidden_dude

finalize() and try-with-resources are not really related.

Finalize() could be used for things such as making sure that a temp file was deleted when no longer used and other such cleanup applications.

In modern Java the same can be accomplished with Phantom references as shown here:

stackoverflow.com/questions/433118...

Collapse
 
tmsanghoclaptrinh profile image
Sáng Minh Trần