Difference between final, finally and finalize in Java

In this Java tutorial, learn about difference between final, finally and finalize in detail. In short, final is a keyword, finally is a block and finalize is a method. They have their own very specific purpose in Java programs.

Let’s discuss each keyword with example.

1. Java final keyword

The final keyword makes a Java variable, method or class as final (it cannot be changed once it is initialized).

1.1. final variable

A variable declared as final cannot be assigned another value after it has been initialized first time. The initialization of final variable may happen at two places only…

  1. While declaring the final variable
    public class MyClass 
    {
    	public final String VERSION = "1.0"; 
    }
    
  2. Inside constructor if variable is not initialized during declaration
    public class MyClass 
    {
    	public final String VERSION; 
    	
    	public MyClass() 	//constructor
    	{
    		this.VERSION = "1.0";
    	}
    }
    

Once final variable is initialized, we cannot change it’s value anywhere in the program. Attaempting to make a change will result in compilation error – The final field MyClass.VERSION cannot be assigned.

1.2. final method

A final method cannot be overridden in child class. If we want a method to be final and shall not be ovverridden by child classes, we need to declare it final.

public class ParentClass 
{	
	public final void showMyName() 
	{
		System.out.println("In ParentClass");
	}
}

Attempting to override showMyName() method will result in compile time error – Cannot override the final method from ParentClass.

Error while overriding final method
Error while overriding final method

1.3. final class

In Java, we cannot inherit a final class. No class can subclass a final class and inherit it’s fields and methods.

public class final ParentClass 
{	
	public void showMyName() 
	{
		System.out.println("In ParentClass");
	}
}
Error while inheriting final class
Error while inheriting final class

2. Java finally block

Java finally block is part of try-catch-finally blocks for exception handling. A finally block is guaranteed to be executed despite whether an exception is thrown or not from the try block.

Ideally, finally block is used to release the resources used in try block. It is mainly intended for resource cleanup activities which help in saving memory and resuorce utilization.

try {
    //open file
    //read file
}
catch(Exception e) {
    //handle exception while reading the file
}
finally {
    //close the file
}

Read More : Java try catch finally blocks

3. Java finalize method

The finalize() method is a special method which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. It is defined in Object class.

protected void finalize() throws Throwable

The main purpose of finalize() method is to perform cleanup actions before the object is irrevocably discarded. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates. The application is not terminated.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

4. Summary – final, finally and finalize

As discussed in above headings, the final, finally and finalize are completely different concepts in Java. They do not have anything in common – except all are Java related concepts.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.