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

  • Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड
    Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड Spiritual
  • Lata Mangeshkar Biography In Hindi | लता मंगेशकर संपूर्ण जीवन परिचय
    Lata Mangeshkar Biography In Hindi | लता मंगेशकर संपूर्ण जीवन परिचय Biography
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans of freedom fighters in Hindi | स्वतंत्रता सेनानियों के प्रसिद्ध नारे हिंदी में General Knowledge
  • States of Matter : Physical Properties of Matter (Solid, Liquid, Gas)
    States of Matter | Physical Properties of Matter (Solid, Liquid, Gas) Science

Difference between final, finally and finalize

Posted on August 16, 2021June 17, 2022 By GeekCer Education 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

What is the key difference between final, finally and finalize in java?

#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

Let’s see example of final keyword with variable in java

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

final keyword with class example

Let’s see example of final keyword with class in java


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

final keyword with method example

Let’s see example of final keyword with method in java.


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

Recommended Articles

  • Checked and unchecked exceptions in java with examples
  • Thread Life Cycle in Java
  • Stream map() in java 8 example
  • Advantages of lambda expressions in java
  • Stack Memory Vs Heap Memory in Details
  • Ways of iterating map in java?

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 Tags:final, finally finalize in java interview questions, Short notes on final finally and finalize in java

Post navigation

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

More Related Articles

Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Difference between Internet and Intranet Difference between Internet and Intranet Differences
Difference Between get() and load() in Hibernate with example Difference Between get() and load() in Hibernate with example Differences
Java 8 Stream map example | Java 8 map(function) parameter Java 8 Stream map example | Java 8 map(function) parameter Java
Multithreading in java Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
Difference between SOAP and REST Difference between SOAP and REST Differences

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • 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