Help the world stop coronavirus! Stay home!

Prev Next

Java / Exception

Difference between final, finally and finalize in Java.

final keyword is used to make a variable or a method or a class as "unchangeable".

Final variable: A variable which is declared as final, its value cannot be changed once it is initialized.

class FinalKeywordExample {
	public static void main(String[] args) {
		final int x = 10;
		x = 200;// Compile Time Error

A final method declared as final can not be overridden or modified in the subclass.

class Parent {
	final void parentMethod() {

class Child extends Parent {
	//Compilation error
	public void parentMethod() {

A final class cannot be extended.

final class Parent {
	void parentMethod() {
//Compilation error
class Child extends Parent {

finally block is used for exception handling along with try and catch blocks. finally block is always executed whether an exception is raised or not and raised exception is handled or not. Most of the time, this block is used to close the resources like database connection, I/O resources.

 try {
      // do stuff
    } catch {
      // handle errors
    } finally {
      // clean up connections etc.

finalize method is a protected method of java.lang.Object class. It is inherited by every class you create in Java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some cleanup operations on an object before it is removed from the memory.

public class FinalizeTest {

	public static void main(String[] args) throws Throwable {
		FinalizeTest fTest = new FinalizeTest();
		FinalizeTest fTest2 = new FinalizeTest();
		fTest = null;
		fTest2 = null;

		System.out.println("Main Thread complete.");

	protected void finalize() throws Throwable {
		System.out.println("finalize method overriden");


❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

How are the exceptions handled in java? Difference between Error and Exception in Java. What are checked and unchecked exceptions? How do we handle more Than One Type of Exception using catch block in Java? What happens if an exception is thrown from the finally or catch block in Java? Will the finally block be executed when the catch clause throws exception in Java? What is a user defined/custom exception in Java? How to create unchecked exception in Java? What will happen to the Exception object after exception handling in Java? How does finally block differ from finalize() method in Java? Does a finally block always run in Java? Does return statement allow finally block to execute in Java? Should a catch block always follow try block in Java for Exception handling? Explain finally block in Java. What is an Exception in Java? In Java 7, can we catch by grouping exceptions that are hierarchically related? What is try-with-resources statement in Java? Can we have multiple resources inside a try-with-resources in Java? What is the order the resources are closed using try-with-resources? Advantages of using try-with-resources in Java. What happens when the AutoCloseable resource is null in Java Try-With-Resource? Difference between Error and runtime exceptions in Java. Difference between throw and throws clause in Java. Can try block exist without any catch and finally block in Java? Will the resources be closed at finally/catch in try with resources? Order of closing resources in try with resources Java statement. What is exception chaining in Java? What is stack trace? What is the order of catch blocks when catching more than one exception? What are the improvements on Try-With-Resources in Java9? Can we have an empty catch block? Can we use FileNotFoundException and IOException in Java multi catch? Important methods of Java Exception Class. When do you subclass an exception? Can you catch OutOfMemoryError? Can you catch java.lang.Error? Give few examples of checked exceptions. Give few examples of unchecked exceptions. What is OutOfMemoryError in java? What is StackOverflowError in java? Explain exception handling when overriding a method? Can overridden method throw RuntimeException when original method throw ArithmeticException? Can we keep other statements in between try, catch and finally blocks? Can I write only try block without any catch and finally block? Explain Throwable class. Java Exception Class hierarchy. Difference between final, finally and finalize in Java. What is rethrowing an exception? Explain the rules of Exception Handling in terms of Method Overriding? Types of OutOfMemoryError in Java. Name some of the tools for probing Java Memory Leaks? Which exception advised not to be thrown from close method when implementing AutoCloseable? Which is the child interface of Autocloseable? What does close method throw: Closeable vs AutoCloseable? How do I rethrow an exception with custom message without losing stack trace?
Show more question and Answers...


Comments & Discussions