Java programming involves a myriad of keywords, each serving distinct purposes to enhance the functionality and robustness of the code. Among these, final
, finally
, and finalize
often cause confusion due to their similar nomenclature. However, they serve entirely different purposes. This article will elucidate the differences between these keywords, their uses, and practical examples to clarify their roles in Java programming.
Introduction
In Java, final
, finally
, and finalize
are keywords with distinct functions:
-
final
: A keyword used in variable, method, and class declarations to denote constants, prevent method overriding, and inheritance. -
finally
: A block used in exception handling to execute code regardless of whether an exception is thrown or not. -
finalize
: A method used to perform cleanup operations before an object is garbage collected.
Understanding the differences and applications of these keywords is crucial for writing effective Java code.
The final
Keyword
Purpose
The final
keyword is versatile and can be applied to variables, methods, and classes.
Use Cases
-
Final Variables: When applied to a variable, the
final
keyword makes it a constant, meaning its value cannot be changed once assigned. - Final Methods: When applied to a method, it prevents the method from being overridden by subclasses.
- Final Classes: When applied to a class, it prevents the class from being subclassed.
Code Examples
Final Variables
public class Constants {
public static final int MAX_USERS = 100;
public static final String APP_NAME = "MyApp";
}
In this example, MAX_USERS
and APP_NAME
are constants that cannot be changed.
Final Methods
public class Parent {
public final void display() {
System.out.println("This is a final method.");
}
}
public class Child extends Parent {
// This will cause a compile-time error
// public void display() {
// System.out.println("Attempting to override a final method.");
// }
}
Here, the display
method in the Parent
class cannot be overridden by the Child
class.
Final Classes
public final class Utility {
public static void performTask() {
System.out.println("Performing a task.");
}
}
// This will cause a compile-time error
// public class AdvancedUtility extends Utility {
// }
The Utility
class cannot be subclassed due to the final
keyword.
The finally
Block
Purpose
The finally
block is used in exception handling to execute code that must run regardless of whether an exception is thrown or caught.
Use Cases
- Resource Management: Ensuring resources like files and database connections are closed properly.
- Cleanup Operations: Performing necessary cleanup actions after try-catch blocks.
Code Example
public class FileOperations {
public void readFile(String filePath) {
FileReader fileReader = null;
try {
fileReader = new FileReader(filePath);
// Perform file operations
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Failed to close the file: " + e.getMessage());
}
}
}
}
}
In this example, the finally
block ensures that the FileReader
resource is closed regardless of whether an exception occurs.
The finalize
Method
Purpose
The finalize
method is invoked by the garbage collector before an object is reclaimed. It is used to perform cleanup operations, such as releasing resources.
Use Cases
- Cleanup Operations: Performing cleanup before an object is garbage collected.
Code Example
public class ResourceHolder {
private FileReader fileReader;
public ResourceHolder(String filePath) throws FileNotFoundException {
this.fileReader = new FileReader(filePath);
}
@Override
protected void finalize() throws Throwable {
try {
if (fileReader != null) {
fileReader.close();
}
} finally {
super.finalize();
}
}
}
Here, the finalize
method ensures that the FileReader
is closed before the ResourceHolder
object is garbage collected. However, it's important to note that the finalize
method is deprecated in recent Java versions due to unpredictability and better alternatives like try-with-resources
and explicit resource management.
Summary
In summary, final
, finally
, and finalize
are three distinct keywords in Java with different purposes:
-
final
: Used to declare constants, prevent method overriding, and inheritance. -
finally
: A block used in exception handling to execute necessary code regardless of exceptions. -
finalize
: A method used for cleanup operations before an object is garbage collected (now largely deprecated).
Understanding these keywords helps in writing more robust, maintainable, and efficient Java code. By using final
, you can create immutable variables and secure methods and classes. The finally
block ensures resource management and cleanup, while finalize
(despite its deprecation) shows the historical approach to object cleanup before garbage collection.
References:
Top comments (3)
Consider the following Java program:
Will this compile? Runs without errors? Does this print "Value of i is 10"?
More about final variables at final Variables (Java Language Specification)
finalize() and try-with-resources are not really related.
Finalize() could be used for things such as making sure that a temp file was deleted when no longer used and other such cleanup applications.
In modern Java the same can be accomplished with Phantom references as shown here:
stackoverflow.com/questions/433118...
My new post for Java developers ✨
Hope this help. Thank you 🌻
Top 10 GitHub repositories for Java developers ✨
Sáng Minh Trần ・ May 26