Difference between final, finally, and finalize

Updated 1 day ago 3 min read 66 views

3. What is the difference between final, finally, and finalize in Java?

 

1. Final 

Definition: A keyword in Java used to apply restrictions on classes, methods, or                              variables. 

Use Cases: 

• For Variables: 

      o Declaring a variable as final makes it a constant, meaning its value cannot be                 changed once assigned. 

• For Methods: 

      o A final method cannot be overridden by subclasses.class 

• For Classes: 

      o A final class cannot be subclassed. 

Key Points: 

• final ensures immutability and prevents modification. 

• Commonly used in design patterns, constants, and to enforce security. 

 

2. Finally 

Definition: A block in Java used for cleanup operations, typically in conjunction with                        try catch statements. 

Use Cases: 

• Executes code that must run regardless of whether an exception occurs or not. 

• Often used to release resources like closing files, database connections, or network        sockets. 

Example: 

try { 

    int result = 10 / 0; // This will throw an ArithmeticException 

}   catch (ArithmeticException e) { 

     System.out.println("Caught an exception."); 

} finally { 

    System.out.println("This block always executes."); 

Key Points: 

• finally is optional but provides a way to guarantee cleanup. 

• It always executes, even if: 

    o An exception is thrown and caught. 

    o A return statement is encountered in try or catch. 

 

3. Finalize 

Definition: A method in Java, part of the Object class, used for garbage collection                           cleanup before an object is destroyed. 

Use Cases: 

• Allows developers to perform cleanup operations before an object is removed from memory (though rarely used in modern Java). 

 

Example: 

class FinalizeExample { 

    @Override 

     protected void finalize() throws Throwable { 

        System.out.println("Finalize method called."); 

     }

     public static void main(String[] args) {

        FinalizeExample obj = new FinalizeExample();

        obj = null; // Eligible for garbage collection

        System.gc(); // Requests garbage collection 

     } 

}

Key Points: 

• The finalize method is called by the Garbage Collector before reclaiming an object’s memory. 

• Deprecated in modern Java (since Java 9) and replaced by more robust mechanisms like try-with-resources and explicit cleanup. 

• Unpredictable and not guaranteed to run immediately.

 

Summary Table

 

Featurefinalfinallyfinalize
TypeKeywordBlock in exception handlingMethod in Object class
PurposeTo restrict behavior (immutability, no override/inheritance).Ensure cleanup code always executes.Cleanup before garbage collection.
UsageVariables, methods, classes.try-catch-finally blocks.Overridden for object cleanup (now deprecated).
WhenAt compile-time.At runtime during exception handling.At runtime before object is garbage collected.

Discussion 0

Leave a comment