
Difference between final, finally and finalize is the most asked question related to interview point of view. If you are a programmer then you must know about the difference between these three. We will discuss difference between final, finally and finalize in detail. Let us first look at the basic differences between them in tabular form.
Table of Contents
final Vs finally Vs finalize
What is the key difference between final, finally and finalize in java?
# | final | Finally | Finalize |
---|---|---|---|
1 | final is a keyword. | finally is a block. | finalize is a method. |
2 | We can use final with classes, methods and variables. | We use finally to create a block of code that follows a try block. | The finalize() method is used just before the object is destroyed. |
final keyword in Java
We use final keyword to put restrictions on a class, method or variable.
- If we create a class with final keyword, We can not create a sub-class of that class.
- We can not override the final method.
- We can not change the value of final variable.
Example program of the final keyword in java:
final keyword with variable example
Let’s see example of final keyword with variable in java
public class Example {
public static void main(String[] args) {
final int variable = 200;
}
}
final keyword with class example
Let’s see example of final keyword with class in java
final public class Example {
public static void main(String[] args) {
System.out.println("final keyword with class");
}
}
final keyword with method example
Let’s see example of final keyword with method in java.
class A {
final void method() { // final method
System.out.println("final method.");
}
}
public class B extends A {
void method() { // Unable to override
System.out.println("Unnable to override");
}
}
You can’t make a class abstract and final at the same time. Because an abstract class should be able to be extended, but if you use final with abstract, you won’t be able to. As a result, combination of abstract and final is impossible.
You may declare an object as final, which indicates that once it’s created, you can not assign a different object, however the object’s properties and fields can be changed.
finally block in Java
The finally block is an important tool to prevent resource leakage. We use this block with the try…catch in exception handling. In Java, finally block is used to hold the critical code, the control always comes to finally block whether the exception is handled or not.
Let’s see the syntax that we use for the finally block
public class Example {
public static void main(String[] args) {
try {
// Code here
} catch (Exception ex) {
// Handle exception here
} finally {
// Always executed this code block
}
}
}
The finally block is optional, although writing one after a try…catch block is a recommended practice.
finalize method in Java
A subclass overrides the finalize method to dispose of system resources or do other cleanup. The finalize method may make the object available again to other threads. Java Virtual Machine invokes finalize method only once for a given object
The finalize method belongs to the Object class and most importantly the garbage collector invokes this method just before discarding the object.
public class Example {
protected void finalize() {
// Free resources here
super.finalize();
}
}
As you can see, the finalize method is protected, which means you can’t use it outside of the class.
In conclusion, you learned the basic idea about finalize keyword, finalize method, and finally block with sample syntax. Hope you are now able to understand the differences between the above three.
References:
https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Recommended Articles
- Checked and unchecked exceptions in java with examples
- Thread Life Cycle in Java
- Stream map() in java 8 example
- Advantages of lambda expressions in java
- Stack Memory Vs Heap Memory in Details
- Ways of iterating map in java?