Criteria final finally finalize
Category Modifier Block Method
Usage
  • Used with classes to prevent inheritance.
  • Used with methods to prevent overriding in a subclass.
  • Used with variables to make them constant.
Associated with the try-catch block. It ensures that the finally block is executed after the try block, irrespective of whether an exception is thrown or not. It’s a method of the Object class that the Garbage Collector calls to free the memory occupied by an object before reclaiming it.
Importance Used for defining an entity’s behavior that cannot be changed further. Used to ensure that certain actions, often cleanup, are always performed regardless of any exception occurrence. Allows an object to clean up resources or perform other cleanup operations before it’s garbage-collected.

Example: Difference Between final, finally, and finalize in Java

public class FinalFinallyFinalizeDemo {

    // Using 'final' keyword with a variable
    final int MAX_VALUE = 100;

    public void someMethod() {
        // Using 'finally' with a try-catch block
        try {
            int result = 10 / 0;  // This will throw an exception
        } catch (ArithmeticException e) {
            System.out.println("Exception caught: " + e.getMessage());
        } finally {
            System.out.println("This will always execute, regardless of an exception.");
        }
    }

    // Using 'finalize' to define cleanup behavior
    @Override
    protected void finalize() throws Throwable {
        System.out.println("Finalize method called. Cleanup can be done here.");
        super.finalize();
    }

    public static void main(String[] args) {
        FinalFinallyFinalizeDemo demo = new FinalFinallyFinalizeDemo();

        System.out.println("MAX_VALUE: " + demo.MAX_VALUE);

        demo.someMethod();

        // Hinting JVM to run garbage collector (For demonstration purposes only, not a best practice)
        demo = null;
        System.gc();
    }
}

Output:

MAX_VALUE: 100
Exception caught: / by zero
This will always execute, regardless of an exception.
Finalize method called. Cleanup can be done here.

Explanation:

1. final: It is a keyword in Java. When used with a variable, it makes the variable constant, meaning its value cannot be changed once assigned. When used with a method, it indicates that the method cannot be overridden. When used with a class, it means the class cannot be subclassed.

2. finally: It is a block used with the try-catch to define a block of code that always executes, regardless of whether an exception was thrown or caught.

3. finalize: It is a method in Java and is part of the java.lang.Object class. It is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method is used to define cleanup actions before the object is removed from memory.

4. In the provided example:

final: The MAX_VALUE variable is declared as final and hence cannot be changed once assigned.

finally: A division by zero operation is attempted, which throws an exception caught in the catch block. The finally block executes after the try or catch block.

finalize: We hint the JVM to run the garbage collector using System.gc() after setting our demo object to null. This leads to the finalize method being called, where we can put our cleanup actions.

Note: Using finalize is not recommended in modern Java as there are better alternatives (like the AutoCloseable interface) and the behavior of finalize can be unpredictable.

In summary, while all three concepts sound similar, they serve very different purposes in Java. final is about immutability, finally is about ensuring code execution, and finalize is about cleanup before garbage collection.