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!
try {
// Code that may throw an exception
} catch (Exception e) {
// Exception handling code
} finally {
// Cleanup code that always executes
}
@Override
protected void finalize() throws Throwable {
// Finalization code
super.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.
final int x = 10; // Once initialized, the value of x cannot be changed
final class MyClass { // MyClass cannot be subclassed
// Class implementation
}
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:
// 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.
@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.final | finally | finalize() |
---|---|---|
Keyword | Block 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 { ... } |