---
title: "What are the differences between final, finally, and finalize in Java?"  
description: "What are the differences between final, finally, and finalize in Java?"  
author: "Ravi Vishwakarma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/interview/33951/what-are-the-differences-between-final-finally-and-finalize-in-java  
category: "java"  
tags: ["java", "javac"]  
reading_time: 5 minutes  

---

# What are the differences between final, finally, and finalize in Java?

### `final` Keyword

The `final` the keyword is used to define constants and prevent method overriding, and inheritance. It can be applied to variables, methods, and classes.

**Read more** - [**What is the difference between static and final keywords in Java?**](https://www.mindstick.com/interview/33950/what-is-the-difference-between-static-and-final-keywords-in-java)

### `finally` Block

The `finally` block is used in conjunction with `try` and `catch` blocks for exception handling. It ensures that a block of code is executed regardless of whether an exception is thrown or not. This is typically used for cleanup activities like closing files, releasing resources, etc.

```java
public class Example {
    public static void main(String[] args) {
        try {
            int data = 25 / 5;
            System.out.println(data);
        } catch (ArithmeticException e) {
            System.out.println("Exception caught");
        } finally {
            System.out.println("Finally block is executed");
        }
    }
}
```

### `finalize` Method

The finalize method is used to perform cleanup operations before an object is garbage collected. This method is defined in the Object class and can be overridden by subclasses. However, it is generally not recommended to rely on finalizing it for cleanup due to its unpredictability and performance costs.

```java
public class Example {
    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("finalize method called");
        } finally {
            super.finalize();
        }
    }

    public static void main(String[] args) {
        Example obj = new Example();
        obj = null;

        // Requesting JVM to call Garbage Collector method
        System.gc();
    }
}
```

## Summary of Differences:

| Feature | `final` | `finally` | `finalize` |
| --- | --- | --- | --- |
| **Purpose** | To define constants, prevent method overriding, and inheritance | To execute code regardless of exception handling | To perform cleanup before garbage collection |
| **Usage** | Applied to variables, methods, and classes | Used in a `try-catch` block | Method that can be overridden in a class |
| **Context** | Compile-time constant and inheritance control | Ensures code execution after try-catch block | Invoked by the garbage collector before object is reclaimed |
| **Example** | `final int x = 10;` `final void method() {}` `final class MyClass {}` | `try { ... } catch (Exception e) { ... } finally { ... }` | `protected void finalize() throws Throwable { ... }` |

[**Bitwise AND, OR, XOR, and left/right shift operations.**](https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations)

[**What is the difference between JDK, JRE, and JVM?**](https://www.mindstick.com/interview/33943/what-is-the-difference-between-jdk-jre-and-jvm)

[**Discuss the differences between deep copy and shallow copy in Java objects.**](https://www.mindstick.com/interview/33945/discuss-the-differences-between-deep-copy-and-shallow-copy-in-java-objects)

## Answers

### Answer by Ravi Vishwakarma

### `final` Keyword

The `final` the keyword is used to define constants and prevent method overriding, and inheritance. It can be applied to variables, methods, and classes.

**Read more** - [**What is the difference between static and final keywords in Java?**](https://www.mindstick.com/interview/33950/what-is-the-difference-between-static-and-final-keywords-in-java)

### `finally` Block

The `finally` block is used in conjunction with `try` and `catch` blocks for exception handling. It ensures that a block of code is executed regardless of whether an exception is thrown or not. This is typically used for cleanup activities like closing files, releasing resources, etc.

```java
public class Example {
    public static void main(String[] args) {
        try {
            int data = 25 / 5;
            System.out.println(data);
        } catch (ArithmeticException e) {
            System.out.println("Exception caught");
        } finally {
            System.out.println("Finally block is executed");
        }
    }
}
```

### `finalize` Method

The finalize method is used to perform cleanup operations before an object is garbage collected. This method is defined in the Object class and can be overridden by subclasses. However, it is generally not recommended to rely on finalizing it for cleanup due to its unpredictability and performance costs.

```java
public class Example {
    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("finalize method called");
        } finally {
            super.finalize();
        }
    }

    public static void main(String[] args) {
        Example obj = new Example();
        obj = null;

        // Requesting JVM to call Garbage Collector method
        System.gc();
    }
}
```

## Summary of Differences:

| Feature | `final` | `finally` | `finalize` |
| --- | --- | --- | --- |
| **Purpose** | To define constants, prevent method overriding, and inheritance | To execute code regardless of exception handling | To perform cleanup before garbage collection |
| **Usage** | Applied to variables, methods, and classes | Used in a `try-catch` block | Method that can be overridden in a class |
| **Context** | Compile-time constant and inheritance control | Ensures code execution after try-catch block | Invoked by the garbage collector before object is reclaimed |
| **Example** | `final int x = 10;` `final void method() {}` `final class MyClass {}` | `try { ... } catch (Exception e) { ... } finally { ... }` | `protected void finalize() throws Throwable { ... }` |

[**Bitwise AND, OR, XOR, and left/right shift operations.**](https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations)

[**What is the difference between JDK, JRE, and JVM?**](https://www.mindstick.com/interview/33943/what-is-the-difference-between-jdk-jre-and-jvm)

[**Discuss the differences between deep copy and shallow copy in Java objects.**](https://www.mindstick.com/interview/33945/discuss-the-differences-between-deep-copy-and-shallow-copy-in-java-objects)


---

Original Source: https://www.mindstick.com/interview/33951/what-are-the-differences-between-final-finally-and-finalize-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
