Difference Between Final, Finally and Finalize

Ever wondered about the differences between final, finally, and finalize in Java? They might sound alike, but each holds its own significance in Java programming. In this blog post, we'll unravel these concepts in plain language, making it easier for you to grasp their nuances and write better Java code. Let's dive in and clarify these Java essentials!

Difference Between Final, Finally and Finalize

1. final

  • 'final' is a keyword in Java that can be applied to variables, methods, and classes.
  • When applied to a variable, 'final' indicates that its value cannot be changed once initialized. In the case of a method, 'final' denotes that the method cannot be overridden by subclasses. And when applied to a class, 'final' signifies that the class cannot be subclassed.
Example:

final int x = 10;  // Once initialized, the value of x cannot be changed
final class MyClass  // MyClass cannot be subclassed
       // Class implementation
}

2. finally

  • 'finally' is a block associated with exception handling in Java, typically used in conjunction with 'try-catch' blocks.
  • The 'finally' block is executed whether an exception is thrown or not. It's useful for cleanup actions, such as closing resources (like files or database connections) regardless of whether an exception occurred.
Example:

try {
     // Code that may throw an exception
} catch (Exception e) {
    // Exception handling code
} finally {
   // Cleanup code that always executes
}

3. finalize

  • 'finalize()' is a method defined in the 'Object' class, which serves as the superclass for all Java classes.
  • This method is called by the garbage collector before an object is reclaimed for memory when it is no longer reachable.
  • It's important to note that 'finalize()' is considered somewhat deprecated in modern Java development, as it's generally not relied upon for resource cleanup due to uncertainties regarding when it will be executed.
Example:

@Override
protected void finalize() throws Throwable {
        // Finalization code
        super.finalize();
}

Difference Between Final, Finally and Finalize

The table below highlights the primary difference between final, finally, and finalize in Java.

finalfinally
finalize()
KeywordBlock associated with exception handling 
Method defined in the 'Object' class
Applied to variables, methods, and classes Executed whether an exception is thrown or not 
Called by the garbage collector before object reclamation
Indicates immutability (for variables), non-overrideability (for methods), and non-subclassability (for classes) Useful for cleanup actions, such as resource closing 
Performs finalization tasks before object destruction
Example:
final int x = 10; 
Example:
try { ... }
catch (Exception e) {
 ... } finally { ... } 
Example: 

@Override

protected void finalize()

throws Throwable { ... }

Conclusion

in summary, "final" serves to define constants, prevent method overriding, or restrict subclassing. On the other hand, "finally" is a block used for cleanup actions within exception handling. Lastly, "finalize()" is a method invoked by the garbage collector before reclaiming memory, but its use is discouraged due to reliability concerns. Understanding these distinctions is essential for writing robust and maintainable Java code.
Next Post Previous Post
No Comment
Add Comment
comment url