Differences between final, finally and finalize
Introduction
Let’s see the differences between final, finally, and finalize in Java programming language.
final
final is a keyword that can be used with a variable or a method or a class. If a final keyword is used with a variable then the variable is considered constant. We cannot change the value and it can be initialized only once.
public static final int NO_OF_MONTHS = 12;
If we try to assign a new value to the above constant we will get an error. The error message is displayed below:
The final field JavaExample.NO_OF_MONTHS cannot be assigned
If the final keyword is used with the method then that method cannot be overridden.
public final void finalMethod() { //final method }
If we try to override this method we will get an error message is displayed below:
Cannot override the final method from JavaExample
– overrides
com.testingdocs.sample.programs.JavaExample.finalMethod
When the final keyword is used with a class then we cannot extend or subclass that class.
public final class JavaExample {}
If we try to subclass this class, we will get an error message displayed below:
The type JavaSubExample cannot subclass the final class JavaExample
finally
finally is used along with try/ catch block in java for exception handling. Also, for exception handling in java, we need to use try and catch block or finally block. We cannot have a simple try block in Java. For example
try{ }
We will get Syntax error, insert “Finally” to complete BlockStatements
The finally block is guaranteed to be executed even if you get exception or not. The finally block will execute for sure at the end. That’s the reason, this block is used for cleanup code like closing streams, network connections, database connections, etc.
Below program exception will be thrown :
package com.testingdocs.sample.programs; public final class JavaExample { static int a; public static void main(String[] args) { finallyDemo(); } public static void finallyDemo() { try{ a= 1/0 ; } catch(Exception e) { System.out.println(e.getMessage()); } finally { System.out.println("Anywayz,This will be executed "); }; } }
Output:
/ by zero
Anywayz,This will be executed
Below code no exception will be thrown, but the finally will be executed as shown below:
package com.testingdocs.sample.programs; public final class JavaExample { static int a; public static void main(String[] args) { finallyDemo(); } public static void finallyDemo() { try{ a= 1/1 ; } catch(Exception e) { System.out.println(e.getMessage()); } finally { System.out.println("Anyway,this will be executed "); }; } }
Output:
Anyway,this will be executed
finalize
It is a method of the Object class that can be overridden by a subclass.
Object
is the root of the class hierarchy. Every class has Object
as a superclass. All objects, including arrays, implement the methods of this class.Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. The general contract of finalize is that it is invoked if and when the JVM has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
The Java language doesn’t guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and the finalization of that object terminates.
After the finalize method has been invoked for an object, no further action is taken until the JVM has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize method is never invoked more than once by a JVM for any given object. Any exception thrown by the finalize method causes the finalization of this object to be halted but is otherwise ignored.
—
Java Tutorial
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :