On this page
  1. finalize
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

finalize

Prev: `Object wait notify` | Up: builtins | Next: stdlib
Rel: AutoCloseable & try-with-resources, builtins, Classes, Records & Sealed Hierarchies
Ref: Java Docs — finalize
Tags: #public #prog #java #builtins #tier/legacy


Availability

Built-in / java.lang (java.lang Deprecated Method) — available with zero import statements.

Syntax & Overview

protected void finalize() throws Throwable  /* Deprecated for removal */

Unpredictable GC-driven finalizer hook deprecated for removal since Java 18 (JEP 421).

Legacy / Avoid: Prefer AutoCloseable & try-with-resources in all modern Java.

Notes & Pitfalls

Example

// Replace finalize() with AutoCloseable + try-with-resources:
try (var stream = new java.io.ByteArrayInputStream(new byte[]{1, 2})) {
    assert stream.read() == 1;
}

Linked from

Related notes