final finally and finalize in Java
In Java, final
, finally
, and finalize
are distinct keywords used in different contexts. Let’s explore the differences between them:
1. final
:
final
is a keyword used to declare a constant, mark a method as not overrideable, or indicate that a class cannot be subclassed (can’t inherited).- When applied to a variable, it means the variable’s value cannot be changed once assigned.
- When applied to a method, it means the method cannot be overridden by subclasses.
- When applied to a class, it means the class cannot be subclassed (can’t inherited).
Example:
Example
final int constantValue = 10;
final class FinalClass {
// Code for the final class
}
class Example {
final void finalMethod() {
// Code for the final method
}
}
2. finally
:
finally
is used in a try-catch-finally block to define a block of code that will be executed whether an exception is thrown or not.- It is typically used for cleanup operations or releasing resources, ensuring that certain code is executed regardless of whether an exception occurs.
Example:
Example
try {
// Code that may throw an exception
} catch (Exception e) {
// Handle the exception
} finally {
// Code in this block will be executed regardless of whether an exception occurred or not
// This is useful for cleanup operations or resource release
}
3. finalize
:
finalize
is a method in theObject
class that can be overridden by a class to perform cleanup operations before an object is garbage-collected.- It is called by the garbage collector before reclaiming the memory occupied by the object.
- However, it’s important to note that using
finalize
for resource cleanup is discouraged in modern Java development due to its unpredictable timing and other issues.
Example:
Example
class ResourceHolder {
// Some resource or state to be cleaned up
private String resource;
public ResourceHolder(String resource) {
this.resource = resource;
}
@Override
protected void finalize() throws Throwable {
try {
// Cleanup operations, e.g., closing a file
System.out.println("Finalizing: " + resource);
} finally {
// Call the finalize method of the superclass
super.finalize();
}
}
}
In summary:
final
: Used for constants, methods, or classes to indicate immutability or prevent further modification or extension.finally
: Used in exception handling to define a block of code that is always executed, regardless of whether an exception is thrown or not.finalize
: A method in theObject
class that can be overridden for cleanup operations before an object is garbage-collected; however, its usage is discouraged in modern Java development.
Here’s a tabular representation of the differences between final
, finally
, and finalize
in Java:
Feature | final | finally | finalize |
---|---|---|---|
Keyword Usage | Variable, Method, Class | Exception Handling (try-catch-finally) | Method Override in Object class |
Immutability | Implies immutability for variables, methods, or classes. | Not related to immutability. | Not related to immutability. |
Inheritance | Prevents method overriding in subclasses, prevents class extension, or marks a constant. | Not related to inheritance. | Overrides finalize method in Object class. |
Try-Catch Blocks | Not used in try-catch blocks. | Used to define code that always executes, whether an exception is thrown or not. | Not used in try-catch blocks. |
Resource Cleanup | Not related to resource cleanup. | Used for cleanup operations or resource release in exception handling. | Used for cleanup operations before an object is garbage-collected. |
Timing of Execution | Executed at different points in the code depending on the usage. | Executed in the context of exception handling, always executed. | Executed by the garbage collector before an object is reclaimed. |
Deprecated | No, final is a fundamental part of Java. | No, finally is a fundamental part of Java. | Yes, finalize is deprecated in Java 9 due to its inherent issues. |
Example | final int constantValue = 10; <br>final class FinalClass { } <br>final void finalMethod() { } | try { /* code */ } catch (Exception e) { /* handle exception */ } finally { /* cleanup code */ } | @Override <br>protected void finalize() throws Throwable { /* cleanup code */ super.finalize(); } |
This table summarizes the key differences in usage, purpose, and characteristics of final
, finally
, and finalize
in Java.