The variable cannot be changed after a final keyword has been applied to it.
When a final variable is declared, it must be initialized.
Final variables are declared in UPPERCASE according to a widely used coding practice.
The final variable doesn't take up memory for each individual instance.
Final Methods
When a method in a class is marked as final, its subclass cannot override it.
The compiler has the ability to "inline" small methods that are marked as final, reducing function call overhead and enhancing performance.
Calls to overridden methods are dynamically resolved, but a method that has been marked as final cannot be overridden. So, at compile time, the function calling can be resolved.
Final Class
A class cannot be inherited by any subclass if it is marked as final.
When a class is declared final, all of its methods are also automatically declared final.
A class cannot be both "final" and "abstract" declared at the same time.
Example of Final keyword
finalclass vehicles{}
class Nano extends vehicles{ // The Nano class cannot be subclass the final class vehiclesvoid run(){
System.out.println("Running");
}
publicstaticvoid main(String args[]){
Nano obj=new Nano();
obj.run();
}
}
Output:
Compile Time Error
Definition of Finally
The try/catch block is always connected to the "finally" block in Java code.
After the try/catch block and before the code that follows it, the "finally" block is executed.
No matter if the exception is thrown or not, the "finally" block will be executed.
The "finally" block is executed whenever an exception is thrown and no catch block can be found to handle it.
The "finally" block is executed just before the method returns to the caller when it exits the try/catch block as a result of an uncaught exception or an explicit return stateme nt.
The "finally" block is used to release memory or clean up the resources used in the "try" block. Although writing the "finally" block after the try/catch block is not required, but it is a good practice.
Example of Finally keyword
publicclass Main {
publicstaticvoid main(String args[]){
try{ // try block int arr[]=newint[10];
arr[15]=15/0;
System.out.println(arr[15]);
}
catch(ArithmeticException e){
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
finally {
System.out.println("The finally block is always executed.");
}
System.out.println("This Code after finally block");
}
}
Output:
java.lang.ArithmeticException: / by zero
The finally block is always executed.
This Code after finally block
Definition of Finalize
A method in an object class is called finalize. A file handle or other non-java resource may be held by an object, in which case it needs to be released before being destroyed.
The garbage collector calls this method before completely destroying the object.
Before the object is destroyed, this method cleans up the area around it.
Example of Finalize keyword
publicclass Main {
publicstaticvoid main(String[] args) {
String str = "Hello India!";
str = null;
System.gc(); // using gc() to call the garbage collector
System.out.println("Garbage Collection End");
}
@Override
protectedvoid finalize(){
System.out.println("Finalize method is called.");
}
}
Output:
Garbage Collection End
Difference Between Final, Finally and Finalize in Java
Difference Between Final, Finally and Finalize in Java are mention below
Basis of Differences
FINAL
FINALLY
FINALIZE
Basic
In Java, "Final" is both a "Keyword" and a "access modifier."
In Java, "finally" is a block.
In Java, "finalize" is a method.
Applicable
The keyword "final" can be used with classes, variables, and methods.
The final block is one that is always included in the try-and-catch block.
The method finalize() is applicable to objects.
Keyword Working style
(1) The final variable, once declared, is a constant that cannot be changed.
(2) No subclass may override the final method.
(3) The final class is not inheritable.
(1) Whether or not there is an exception, the finally block executes the crucial code.
(2) The finally block deactivates all of the resources used in the try block.
Before an object is destroyed, the finalize method performs cleaning operations related to it.
Execution
Only when we call the final method is it executed.
When the try-catch block is executed, the finally block is executed. Its execution is not affected by the exception.
The finalize method is invoked just prior to the destruction of the object.
Key Differences Between Final, Finally, and Finalize
Finally is an access modifier, finalize is a method, and finalize is a block.
The keyword final applies to classes, variables, and methods. Finally is a block associated with the try catch block that is used to handle exceptions, and finalize is a method that only works with objects.
Once declared as final, a variable becomes constant and cannot be reassigned, a method declared as final cannot be overridden, and a class declared as final can never be inherited. Finally, the finally block is used to clear out the resources used by the try and catch blocks. the finalize method plays a crucial role in managing an object's resources and ensuring their proper cleanup before the object is destroyed. This article explores the significance of the finalize method in Java Exception Handling and its vital role in resource management.
Post your comment