☕Understanding `final`, `finally`, and `finalize` in Java
Tags: java, learning, keywords, beginners
Published: 2024-05-24 13:15:51

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imoywhmnls8y21ie9y9j.png)


## 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

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

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

#### Final Methods

```java
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.");
    // }
}
```

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

#### Final Classes

```java
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 {
// }
```

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

```java
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());
                }
            }
        }
    }
}
```

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

```java
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();
        }
    }
}
```

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:**

- [Oracle Java Documentation](https://docs.oracle.com/javase/tutorial/essential/exceptions/)
- [GeeksforGeeks - Java final, finally and finalize](https://www.geeksforgeeks.org/final-finally-and-finalize-in-java/)
- [Baeldung - Java Keywords](https://www.baeldung.com/java-final)