Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Differences » Difference between final, finally and finalize

  • Python Variable and Python Datatype
    Python Variable and Python Datatype Python
  • Software Performance Testing in Hindi
    Software Performance Testing in Hindi Important
  • Different Types of Loans in India, Eligibility, Documents, Benefits
    Different Types of Loans in India, Eligibility, Documents, Benefits Important
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Top Java Programs for Coder
    Top Java Programs for Coder Java
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • IPL 2022 New Teams Names and Owners
    IPL 2022 New Teams Names and Owners News
  • JDBC Driver in Java
    JDBC Driver in Java Java

Difference between final, finally and finalize

Posted on August 16, 2021April 15, 2022 By admin No Comments on Difference between final, finally and finalize
Difference between final, finally and finalize

Difference between final, finally and finalize is the most asked question related to interview point of view. If you are a programmer then you must know about the difference between these three. We will discuss difference between final, finally and finalize in detail. Let us first look at the basic differences between them in tabular form.

Table of Contents

  • final Vs finally Vs finalize
  • final keyword in Java
    • final keyword with variable example
    • final keyword with class example
    • final keyword with method example
  • finally block in Java
  • finalize method in Java

final Vs finally Vs finalize

#finalFinallyFinalize
1final is a keyword. finally is a block. finalize is a method.
2We can use final with classes, methods and variables. We use finally to create a block of code that follows a try block.The finalize() method is used just before the object is destroyed.

final keyword in Java

We use final keyword to put restrictions on a class, method or variable.

  • If we create a class with final keyword, We can not create a sub-class of that class.
  • We can not override the final method.
  • We can not change the value of final variable.

Example program of the final keyword in java:

final keyword with variable example

public class Example {
  public static void main(String[] args) {
    final int variable = 200;
  }
}

final keyword with class example


final public class Example {
    
  public static void main(String[] args) {
      System.out.println("final keyword with class");
  }
}

final keyword with method example


class A {
  final void method() { // final method
    System.out.println("final method.");
  }
}
public class B extends A {
  void method() { // Unable to override
    System.out.println("Unnable to override");
  }
}

You can’t make a class abstract and final at the same time. Because an abstract class should be able to be extended, but if you use final with abstract, you won’t be able to. As a result, combination of abstract and final is impossible.

You may declare an object as final, which indicates that once it’s created, you can not assign a different object, however the object’s properties and fields can be changed.

finally block in Java

The finally block is an important tool to prevent resource leakage. We use this block with the try…catch in exception handling. In Java, finally block is used to hold the critical code, the control always comes to finally block whether the exception is handled or not.

Let’s see the syntax that we use for the finally block

                  public class Example {
  public static void main(String[] args) {
    try {
      // Code here
    } catch (Exception ex) {
      // Handle exception here
    } finally {
      // Always executed this code block
    }
  }
}

The finally block is optional, although writing one after a try…catch block is a recommended practice.

finalize method in Java

A subclass overrides the finalize method to dispose of system resources or do other cleanup. The finalize method may make the object available again to other threads. Java Virtual Machine invokes finalize method only once for a given object

The finalize method belongs to the Object class and most importantly the garbage collector invokes this method just before discarding the object.

                  public class Example {
                                    
  protected void finalize() {
   // Free resources here
   super.finalize();
  }
}

As you can see, the finalize method is protected, which means you can’t use it outside of the class.

In conclusion, you learned the basic idea about finalize keyword, finalize method, and finally block with sample syntax. Hope you are now able to understand the differences between the above three.

References:
https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Differences, Java

Post navigation

Previous Post: Difference between Jenkins and TeamCity, Maven, Octopus
Next Post: Difference between StringBuffer and StringBuilder

More Related Articles

Difference between TCP and UDP Difference between TCP and UDP Differences
Java Comments Java Comments : Types of java comments, Syntax & Examples Java
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences
Method Overloading in Java Method Overloading in Java Java
Top Java Programs for Coder Top Java Programs for Coder Java
Concurrent collections in Java, details, advantages, examples Concurrent collections in Java, details, advantages, examples Java

Related Posts

  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Java Comments
    Java Comments : Types of java comments, Syntax & Examples Java
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Method Overloading in Java
    Method Overloading in Java Java
  • Top Java Programs for Coder
    Top Java Programs for Coder Java
  • Concurrent collections in Java, details, advantages, examples
    Concurrent collections in Java, details, advantages, examples Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Python Variable and Python Datatype
    Python Variable and Python Datatype Python
  • Software Performance Testing in Hindi
    Software Performance Testing in Hindi Important
  • Different Types of Loans in India, Eligibility, Documents, Benefits
    Different Types of Loans in India, Eligibility, Documents, Benefits Important
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Top Java Programs for Coder
    Top Java Programs for Coder Java
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • IPL 2022 New Teams Names and Owners
    IPL 2022 New Teams Names and Owners News
  • JDBC Driver in Java
    JDBC Driver in Java Java
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved