Difference between final, finally and finalize in Java

In this Java tutorial, learn about the differences between final, finally and finalize in detail.

1. Java final Keyword

The final keyword can be used with class variables, methods or classes. It has a different meaning depending upon it is applied to variable, class or method.

1.1. final Variables

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

  • At the time of declaration
  • Inside the constructor
public class MyClass 
{
  // During declaration
  public final String MAJOR_VERSION = "1"; 
  public final String MINOR_VERSION; 
   
  public MyClass()  
  {
    // in constructor
    this.MINOR_VERSION = "2";
  }
}

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

1.2. final Methods

A final method cannot be overridden in child class.

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 Classes

In Java, we cannot inherit a final class. No class can subclass a final class or inherit its 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 Blocks

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 the try block. It is mainly intended for resource cleanup activities which help in saving memory and resource 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

JEP-421 (Java 18) marked finalization deprecated and for removal in a future release. Maintainers of libraries and applications that rely upon finalization should consider migrating to other resource management techniques such as the try-with-resources statement and cleaners.

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 the 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. Conclusion

As discussed in the above sections, 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 !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode