Skip to content
LOCEN/Ontario · CAStandbyOK/--:--:--EST
M4M4RK_YUportfolio
  • Projects
    ProjectsOverview
    • WorkSelected case studies and write-ups
    • GamesPlayable prototypes and game-dev logs
  • Gallery
    GalleryOverview
    • ArchivePhoto collections and visual experiments
    • ShopPrints, posters, and one-off objects
  • Logs
    LogsOverview
    • BlogLong-form devlogs and field notes
    • NotesShort observations, links, snippets
  • Resources
    ResourcesOverview
    • Tools38 in-browser developer utilities
    • LinksDaily-use dev and design bookmarks
  • About
  • Contact
中文

syndicated · dev.to / @markyu

☕Understanding `final`, `finally`, and `finalize` in Java

Java programming involves a myriad of keywords, each serving distinct purposes to enhance the...

Published
May 24 '24
·
Reading time
3 min read
·
Reactions
13
·
Comments
3
javalearningkeywordsbeginners
View on dev.toDiscuss

Java programming involves a myriad of keywords, each serving distinct purposes to enhance the functionality and robustness of the code. Among these, final, finally, and finalize often cause confusion due to their similar nomenclature. However, they serve entirely different purposes. This article will elucidate the differences between these keywords, their uses, and practical examples to clarify their roles in Java programming.

Image description

Introduction

In Java, final, finally, and finalize are keywords with distinct functions:

  • final: A keyword used in variable, method, and class declarations to denote constants, prevent method overriding, and inheritance.
  • finally: A block used in exception handling to execute code regardless of whether an exception is thrown or not.
  • finalize: A method used to perform cleanup operations before an object is garbage collected.

Understanding the differences and applications of these keywords is crucial for writing effective Java code.

The final Keyword

Purpose

The final keyword is versatile and can be applied to variables, methods, and classes.

Use Cases

  1. Final Variables: When applied to a variable, the final keyword makes it a constant, meaning its value cannot be changed once assigned.
  2. Final Methods: When applied to a method, it prevents the method from being overridden by subclasses.
  3. Final Classes: When applied to a class, it prevents the class from being subclassed.

Code Examples

Final Variables

public class Constants {
    public static final int MAX_USERS = 100;
    public static final String APP_NAME = "MyApp";
}

In this example, MAX_USERS and APP_NAME are constants that cannot be changed.

Final Methods

public class Parent {
    public final void display() {
        System.out.println("This is a final method.");
    }
}

public class Child extends Parent {
    // This will cause a compile-time error
    // public void display() {
    //    System.out.println("Attempting to override a final method.");
    // }
}

Here, the display method in the Parent class cannot be overridden by the Child class.

Final Classes

public final class Utility {
    public static void performTask() {
        System.out.println("Performing a task.");
    }
}

// This will cause a compile-time error
// public class AdvancedUtility extends Utility {
// }

The Utility class cannot be subclassed due to the final keyword.

The finally Block

Purpose

The finally block is used in exception handling to execute code that must run regardless of whether an exception is thrown or caught.

Use Cases

  1. Resource Management: Ensuring resources like files and database connections are closed properly.
  2. Cleanup Operations: Performing necessary cleanup actions after try-catch blocks.

Code Example

public class FileOperations {
    public void readFile(String filePath) {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(filePath);
            // Perform file operations
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    System.out.println("Failed to close the file: " + e.getMessage());
                }
            }
        }
    }
}

In this example, the finally block ensures that the FileReader resource is closed regardless of whether an exception occurs.

The finalize Method

Purpose

The finalize method is invoked by the garbage collector before an object is reclaimed. It is used to perform cleanup operations, such as releasing resources.

Use Cases

  1. Cleanup Operations: Performing cleanup before an object is garbage collected.

Code Example

public class ResourceHolder {
    private FileReader fileReader;

    public ResourceHolder(String filePath) throws FileNotFoundException {
        this.fileReader = new FileReader(filePath);
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (fileReader != null) {
                fileReader.close();
            }
        } finally {
            super.finalize();
        }
    }
}

Here, the finalize method ensures that the FileReader is closed before the ResourceHolder object is garbage collected. However, it's important to note that the finalize method is deprecated in recent Java versions due to unpredictability and better alternatives like try-with-resources and explicit resource management.

Summary

In summary, final, finally, and finalize are three distinct keywords in Java with different purposes:

  • final: Used to declare constants, prevent method overriding, and inheritance.
  • finally: A block used in exception handling to execute necessary code regardless of exceptions.
  • finalize: A method used for cleanup operations before an object is garbage collected (now largely deprecated).

Understanding these keywords helps in writing more robust, maintainable, and efficient Java code. By using final, you can create immutable variables and secure methods and classes. The finally block ensures resource management and cleanup, while finalize (despite its deprecation) shows the historical approach to object cleanup before garbage collection.


References:

  • Oracle Java Documentation
  • GeeksforGeeks - Java final, finally and finalize
  • Baeldung - Java Keywords

Related reading

java

Understanding the `throw` and `throws` Keywords in Java

Exception handling is a crucial aspect of Java programming, allowing developers to manage and respond...

java

Advanced Java: Simplifying Object Property Copy and Manipulation with BeanUtil

In Java programming, the BeanUtil utility class is a powerful and convenient tool for simplifying the...

database

The True Cost of Poor Data Quality: Why It Matters and How to Improve It

In today’s fast-paced, data-driven world, businesses have more access to data than ever before....

originally published

This post first ran on dev.to. Comments and reactions live there.

Continue on dev.to
PreviousUnderstanding the `throw` and `throws` Keywords in JavaException handling is a crucial aspect of Java programming, allowing developers to manage and respond...
Back to archive
NextKey Considerations for Effective Database Table DesignIntroduction In database design, the structure of tables is a critical element that...
Back to archive
open channel·say hi anytime · 2026
--:--:--EST
get in touch

Saw something here?Tell me about it.

It's a portfolio, not a service · but I read every note — drop a line if anything here resonated, or just to say hi.

Start a conversation

Newsletter

Get the occasional dispatch

Notes and logs from m4rkyu.com — short, dated, no noise. Unsubscribe anytime.

Work

Production builds, games, and visual archives.

  • Projects
  • Games
  • Archive
  • Logs

Resources

Daily-use tools and a personal link library.

  • Search
  • Latest
  • Tools
  • Links
  • Notes
  • Topics
  • RSS
  • JSON feed
  • Shop

Studio

Background, contact, and channels for collaboration.

  • About
  • Contact
  • Changelog
  • Colophon
  • Resumepending

Socials

Find me on the usual feeds.

  • Facebooksoon
  • Instagramsoon
  • YouTubesoon
  • LinkedInsoon
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
Crafted since 2024
ZhenXiao Mark YuZhenXiao Mark Yu
© 2026 ZhenXiao Mark Yu·Ontario, Canada
  • Email
  • GitHub
  • dev.to
  • LinkedIn (soon)
  • Twitter / X (soon)
  • Instagram (soon)
Built with Next.js 16 · React 19 · Tailwind 4

Built with Next.js 16 · React 19 · Tailwind 4