Difference between final, finally, and finalize in Java

1. Introduction

In Java, final, finally, and finalize are three concepts that often cause confusion due to their similar names. However, they serve entirely different purposes. 

final is a keyword used to define an entity that cannot be modified after it's initialized. 

finally is a block used in exception handling that executes code regardless of whether an exception is thrown. 

finalize is a method from the Object class, which the garbage collector calls before object memory is reclaimed.

2. Key Points

1. final can be used to define variables, methods, and classes that cannot be changed, overridden, or inherited.

2. finally is a block that complements the try-catch blocks in exception handling.

3. finalize is a method that is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

4. final is a keyword, finally is a block, and finalize is a method.

3. Differences

final finally finalize
A modifier for non-modifiable entities. A block that executes after try/catch. A method called before an object is garbage collected.
Used with variables, methods, and classes. Used with exception handling. Used as part of the garbage collection process.
Prevents further modification or inheritance. Ensures execution of important code statements. Allows clean up before the object is destroyed.

4. Example


// Example of final
final int NUMBER = 42;

// Example of finally
try {
    // Code that may throw an exception
    int division = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Exception caught!");
} finally {
    System.out.println("This will always be printed, even if there is an exception.");
}

// Example of finalize
public class SampleObject {
    // The finalize method
    protected void finalize() throws Throwable {
        try {
            // Cleanup code or resource release logic
            System.out.println("Finalize method called.");
        } finally {
            super.finalize();
        }
    }
}

// Let's assume SampleObject gets used and is now ready to be garbage collected.

Output:

Exception caught!
This will always be printed, even if there is an exception.
// Finalize method output may not show up in a predictable manner as it depends on the garbage collector.

Explanation:

1. final keyword is shown being used with a variable, making the variable's value unchangeable after initialization.

2. finally block is shown in context with try-catch, to ensure that some code is executed after try-catch, irrespective of an exception.

3. finalize method is shown inside a class definition, showcasing how it can be used to execute cleanup code before an object is garbage collected.

5. When to use?

- Use final when you want to create a constant variable, prevent method overriding, or prevent inheritance.

- Use finally when you have critical code that must be executed regardless of an exception occurring, such as closing resources.

- Use finalize to perform clean up of resources before an object is garbage collected, although it's generally recommended to use other resource management techniques,

Comments