The final, finally and finalize are common terms used in Java programming. The final is a keyword. It can be used for variables, methods or classes. The variables declared as final should be initialized only once. They cannot be changed. As Java is a language that supports Object Oriented Programming, it allows creating classes from existing classes to improve code reusability. Sometimes, it may be necessary to avoid using the existing classes. For that, final can be used. In programming, there can be errors, and it is important to handle them in order to execute the program properly. The finalize is a method called by the garbage collector. So all these terms have different meanings accordingly. The final is a keyword that prevents changing variables, avoids method overriding and avoids extending classes. The finally is a block in exception handling, that will execute whether an exception is thrown or not. The finalize is a method, called by the garbage collector before it destroys the object completely. That is the key difference final finally and finalize in Java.
CONTENTS
1. Overview and Key Difference
2. What is final in Java
3. What is finally in Java
4. What is finalize in Java
5. Similarities Between final finally and finalize in Java
6. Side by Side Comparison – final finally vs finalize in Java in Tabular Form
7. Summary
What is final in Java?
The final is a keyword in Java. As Java supports Object Oriented programming, the subclasses can use the variables and methods of an already existing class. The already existing class is the superclass while the new class is the subclass. If the programmer wants to prevent the variable accessible by other classes, he can declare the variable as ‘final’. For example, assume that there is a variable as p. It is declared as final and initialized the value 10. e.g. final int p= 10. If the p value is changed again to 20, it will cause a compile-time error. The final keyword prevents from changing the value of the variable.
A class can use a method that is already in an existing class. Assume that there is a class called B that has a method display(). The new class is C, and it extends class B. If class C is also having a method called display(), then the original class B display() method is overridden. If the programmer wants to avoid method from overriding, then he can use the keyword finally. e.g. final void display(){ }. Making a method final ensures that the functionality of the method will never be altered.
It is also possible to use the final keyword for a class. The new class cannot inherit variables and methods of a final class. This is useful to increase security. As the class is being prevented from using by the subclasses, the data is protected.
What is finally in Java?
In programming, there can be errors. Errors can cause incorrect outputs or terminates the execution of a program. It is important to use some kind of mechanism to prevent these unexpected results. Errors can be of two types. They are compiled time errors and runtime errors. Compile time errors occur due to syntactical errors. Some common compile-time errors are missing the semicolon, missing curly braces, misspelling identifiers, keywords and undeclared variables. The compiler will not create the .class file till these errors are fixed.
Sometimes there can be programs that compile properly but gives wrong output. They are called runtime errors. Some common runtime errors are diving an integer by zero and accessing an element that is out of bounds of an array. These errors will not cause an error at compile time, but the output is incorrect. An exception is a condition that is caused by a runtime error in the program.
When there is a runtime error, Java creates an exception object and throws it. If the exception object is not caught properly, it will display an error message and will terminate the program. If the programmer wants to continue the execution of the program with the rest of the code, he should catch the exception object and display the necessary message for corrective action. This process is known as exception handling.
In Java, try is used for the code that is likely to cause an error and throw an exception. The catch is used to handle the exception thrown by the try block. There can be multiple catch statements. The finally statements can be used to handle an exception that is not caught by any of previous catch statements. The finally block will execute whether an exception is thrown or not. Refer the given example.
int p = 10, q=5, r= 5;
int answer;
try{
answer = p / (q – r);
}
catch (ArithmeticException e){
System.out.println(“Divided by zero”);
}
finally{
System.out.println(“The finally block is executed”);
}
According to the above example, the value p is divided by zero, and it will cause an exception. Therefore, it is caught by the catch statement. It will print the message, Divided by zero. The finally block will execute whether an exception occurred or not. After the Divided by zero message, the message inside the finally block will display. Therefore, finally is a block used in exception handling.
What is finalize in Java?
In OOP, objects are created using classes. The constructor method can initialize an object when it is declared. The process is known as initialization. Java also has a concept called finalization. The Java runtime is an automatic garbage collector. It automatically frees up the memory resources used by the objects. The garbage collector calls this method before destroying the object.
Some objects might hold non-object resources. One example is a file descriptor. In these situations, the garbage collector calls the finalize method. e.g. finalize(). This method performs clean up processing just before the object is garbage collected.
What is the Similarity Between final finally and finalize in Java?
- All final, finally and finalize in Java are used in Java programming
What is the Difference Between final finally and finalize in Java?
final vs finally vs finalize |
|
final | The final is a keyword in Java that prevents changing variables, avoid method overriding and avoid extending classes. |
finally | The finally is a block in Java exception handling, that will execute whether an exception is thrown or not. |
finalize | The finalize is a method in Java, called by the garbage collector before it destroys the object completely. |
Applicability | |
final | The final is applicable for variables, methods and classes. |
finally | The finally is applicable with a try and catch blocks. |
finalize | The finalize is applicable for objects. |
Summary – final vs finally vs finalize in Java
The final, finally and finalize are common terms used in Java Programming. Their words seem the same, but they have a difference. The final is a keyword that prevents changing variables, avoid method overriding and avoid extending classes. The finally is a block in exception handling, that will execute whether an exception is thrown or not. The finalize is a method, called by the garbage collector before it destroys the object completely. That is the difference between final, finally and finalize in Java Programming.
Download the PDF of final vs finally vs finalize in Java
You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Betweeen final finally and finalize in Java
Reference:
1.Java Basics Part 1 – Q 20)Difference Final Finally Finalize, JavaNoobs365, 13 Mar. 2015. Available here