final keyword

class

On a class it means you forbid to have a child class extending yours.

public final class finalClass

Attribute/Field

final MyObject value = new MyObject() means you won't be able to modify the instance of the object.

value = xxxx won't be allowed, but you still can modify the object itself value.field = "xxx";

Method When you use final on a method, that means you'll forbid child classes that extends your class to override this method.

public final void finalMethod()

It can also be used on arguments, that means you don't allow other to modify the instance of the object you give.

public void myMethod(final MyObject myObject)

The end user won't be able to do myObject = ...


Finally block

finally block has nothing to do with final, it's used when catching Exception to ensure a part of code will be ran, wherever there is an exception or not.

try { ...}
catch {} // Optional
finally { // Code that will be ran, exception or not being thrown}


Finalize method

It's called on every object when it's destroyed (usually garbage collected).

Answer from Michael Laffargue on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-final-finally-and-finalize
Java final, finally and finalize - GeeksforGeeks
3 weeks ago - In Java, the keywords "final", "finally" and "finalize" have distinct roles. final enforces immutability and prevents changes to variables, methods or classes. finally ensures a block of code runs after a try-catch, regardless of exceptions.
Top answer
1 of 5
8

final keyword

class

On a class it means you forbid to have a child class extending yours.

public final class finalClass

Attribute/Field

final MyObject value = new MyObject() means you won't be able to modify the instance of the object.

value = xxxx won't be allowed, but you still can modify the object itself value.field = "xxx";

Method When you use final on a method, that means you'll forbid child classes that extends your class to override this method.

public final void finalMethod()

It can also be used on arguments, that means you don't allow other to modify the instance of the object you give.

public void myMethod(final MyObject myObject)

The end user won't be able to do myObject = ...


Finally block

finally block has nothing to do with final, it's used when catching Exception to ensure a part of code will be ran, wherever there is an exception or not.

try { ...}
catch {} // Optional
finally { // Code that will be ran, exception or not being thrown}


Finalize method

It's called on every object when it's destroyed (usually garbage collected).

2 of 5
2

Final:- It is used in the following cases:

  • If the final keyword is attached to a variable then the variable becomes constant i.e. its value cannot be changed in the program.
  • If a method is marked as final then the method cannot be overridden by any other method.
  • If a class is marked as final then this class cannot be inherited by any other class.
  • If a parameter is marked with final it becomes a read only parameter.

Finally:-

If an exception is thrown in try block then the control directly passes to the catch block without executing the lines of code written in the remainder section of the try block. In case of an exception we may need to clean up some objects that we created. If we do the clean-up in try block, they may not be executed in case of an exception. Thus finally block is used which contains the code for clean-up and is always executed after the try ...catch block.

Finalize:-

It is a method present in a class which is called before any of its object is reclaimed by the garbage collector. finalize() method is used for performing code clean-up before the object is reclaimed by the garbage collector.

๐ŸŒ
Faun
faun.pub โ€บ final-finally-and-finalize-in-java-426fb34a7809
Final, Finally and Finalize in JAVA | by Maleesha Kumarasinghe | FAUN.dev() ๐Ÿพ
December 17, 2024 - The finalize method is part of the garbage collection mechanism in Java. It is called by the garbage collector just before an object is destroyed. The finalize method allows an object to clean up resources before it is removed from memory.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 11 โ€บ difference-between-final-finally-and-finalize-java.html
Difference between final, finally and finalize method in Java
May 25, 2023 - In short final keyword can be used ... Exception handling along with try, catch, throw, and throws. finalize() is a special method in Java that is called by Garbage Collector before reclaiming GC eligible objects....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ differences between final, finally and finalize in java
Differences between Final, Finally and Finalize in Java | Baeldung
January 25, 2024 - Itโ€™s even possible to use it with the try block without any catch block provided we include a finally block. The code will then be executed after the try or after an exception is thrown. We have an in-depth article about exception handling in Java here.
๐ŸŒ
HowDev
how.dev โ€บ answers โ€บ the-difference-between-final-finally-and-finalize-in-java
The difference between final, finally, and finalize in Java
Final restricts inheritance, overriding, and modification. Finally ensures code execution, finalize handles cleanup before garbage collection.
๐ŸŒ
Medium
medium.com โ€บ @sharma0purnima โ€บ understanding-final-finally-and-finalize-in-java-29b19eb17519
Understanding final, finally, and finalize() in Java | by Purnima Sharma | Medium
July 13, 2024 - Understanding the differences between ... code. The final keyword ensures immutability, the finally block guarantees resource cleanup, and the finalize() method provides a mechanism for cleanup before garbage collection...
Find elsewhere
๐ŸŒ
Java Guides
javaguides.net โ€บ 2023 โ€บ 11 โ€บ difference-between-final-finally-and-finalize-in-java.html
Difference between final, finally, and finalize in Java
April 6, 2025 - It is defined in the java.lang.Object class and can be overridden by subclasses. Note: finalize() is now considered deprecated starting from Java 9 and removed in Java 18, so itโ€™s rarely used in modern applications.
๐ŸŒ
Tech Differences
techdifferences.com โ€บ home โ€บ difference between final, finally and finalize in java
Difference Between Final, Finally and Finalize in Java (with Comparison Chart) - Tech Differences
December 25, 2019 - The basic difference between final, finally, and finalize is that final is an access modifier, finally is a block and finalize is a method of an object class.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ final-finally-and-finalize-in-java
final, finally and finalize in Java
July 29, 2021 - public class Tester { public static void main(String[] args) { try{ int a = 10; int b = 0; int result = a/b; }catch(Exception e){ System.out.println("Error: "+ e.getMessage()); } finally{ System.out.println("Finished."); } } }
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ final, finally & finalize in java | 15+ differences (+codes)
Final, Finally & Finalize In Java | 15+ Differences (+Codes) // Unstop
November 15, 2024 - In Java, final defines constants, prevents inheritance, and stops method overriding. Finally ensures code runs after try-catch, and finalize is deprecated for cleanup.
๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ it & software articles โ€บ programming articles โ€บ difference between final, finally, and finalize in java
Difference Between final, finally, and finalize in Java
April 22, 2024 - While final is used to create constants and prevent method overriding or class inheritance, finally is a block that executes after try-catch for guaranteed execution, typically for cleanup.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ final-finally-and-finalize-in-java
All You Need to Know About Final, Finally and Finalize in Java | Edureka
July 11, 2023 - Whenever the final keyword in Java is used with a variable, field or parameter it means that once the reference is passed on or the instantiation is done then its value cannot be changed throughout the execution of the program.
๐ŸŒ
Medium
medium.com โ€บ @amarjeetcs79 โ€บ difference-between-final-finally-finalize-in-java-27b3943f5343
Difference Between final, finally & finalize in Java? | by Amarjeet Kumar Singh | Medium
August 27, 2024 - Description: When a variable is declared as final, it means that its value cannot be changed once it is initialized. It effectively makes the variable a constant. ... Description: A final method cannot be overridden by subclasses.
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-final-vs-finally-vs-finalize
Java Final vs Finally vs Finalize - TechVidvan
November 15, 2023 - Before you destroy your object, the garbage collector will call finalize() to clean it up. Garbage collection is done automatically in Java, which is handled by the JVM and uses the finalize method in Java to release the resources of the object to be destroyed.
๐ŸŒ
Java Concept Of The Day
javaconceptoftheday.com โ€บ home โ€บ difference between final, finally and finalize in java
Difference Between final, finally and finalize In Java
November 11, 2016 - finalize() method is a protected method of java.lang.Object class. It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java keywords โ€บ difference between final, finally and finalize in java
Difference between final, finally and finalize in Java
October 1, 2022 - 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.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-final-finally-and-finalize-in-Java
What is the difference between final, finally, and finalize in Java? - Quora
Answer (1 of 23): final - final keyword can be used with a class, variable or a method. * A variable declared as final acts as constant, which means one a variable is declared and assigned , the value cannot be changed. An object can also be final, which means that the once the object is create...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_keyword_final.asp
Java final Keyword
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods