Unlock AI power-ups β upgrade and save 20%!
Use code STUBE20OFF during your first month after signup. Upgrade now β
By Java by Saurabh Shukla Sir
Published Loading...
N/A views
N/A likes
Get instant insights and key takeaways from this YouTube video by Java by Saurabh Shukla Sir.
Understanding the Finalize Method in Java
π The `finalize()` method is discussed as a mechanism related to Garbage Collection, following the lecture on garbage collection (Lecture 48).
π When an object is created using the `new` keyword, it receives dynamic memory, and its constructor initializes its members, which may point to external resources like printers, files, or databases.
π When an object becomes unreachable (a "garbage block") after its reference variable goes out of scope, the JVM's Garbage Collector (GC) is responsible for releasing its memory.
Purpose and Execution of `finalize()`
π The primary utility of `finalize()` is to perform cleanup operations just before an object's memory is released by the GC, similar to a destructor in C++.
π Cleanup operations include properly closing database connections or ensuring data is written to a hard disk before the external resource pointers inside the object are destroyed.
π The GC thread calls the object's `finalize()` method only once immediately before sweeping out (destroying) the object.
Inheritance and Calling `finalize()`
π Every Java class is a descendant of the `Object` class, which contains a default, empty `finalize()` method.
π If a subclass does not override `finalize()`, the GC will call the version inherited from the `Object` class, which performs no action.
π Unlike C++, there is no automatic chaining of `finalize()` methods in Java; the subclass `finalize()` does not automatically call the superclass's `finalize()`βit must be called explicitly using `super.finalize()`.
Forced Execution and Reliability
β‘οΈ Developers can call `finalize()` manually like any other method (e.g., using `Runtime.getRuntime().runFinalization()`), but this does not guarantee the object's memory release; only the GC triggers memory release.
β‘οΈ Exceptions occurring inside the `finalize()` method are ignored by the Garbage Collector, ensuring the GC continues its process and destroys the object.
β‘οΈ Relying on forced execution is not recommended as it is not reliable; execution is only guaranteed when the GC runs.
Key Points & Insights
β‘οΈ `finalize()` serves as the object's "last wish" to execute critical closing/cleanup code before memory reclamation by the GC.
β‘οΈ If an object holds external resources (e.g., database connections), ensure the code to close these is placed within the `finalize()` method to prevent resource leaks.
β‘οΈ Do not depend on manual execution of `finalize()`; trust the Garbage Collector for invoking cleanup logic just before memory destruction.
πΈ Video summarized with SummaryTube.com on Dec 30, 2025, 14:53 UTC
Find relevant products on Amazon related to this video
As an Amazon Associate, we earn from qualifying purchases
Full video URL: youtube.com/watch?v=e5TINddM-GM
Duration: 24:04
Get instant insights and key takeaways from this YouTube video by Java by Saurabh Shukla Sir.
Understanding the Finalize Method in Java
π The `finalize()` method is discussed as a mechanism related to Garbage Collection, following the lecture on garbage collection (Lecture 48).
π When an object is created using the `new` keyword, it receives dynamic memory, and its constructor initializes its members, which may point to external resources like printers, files, or databases.
π When an object becomes unreachable (a "garbage block") after its reference variable goes out of scope, the JVM's Garbage Collector (GC) is responsible for releasing its memory.
Purpose and Execution of `finalize()`
π The primary utility of `finalize()` is to perform cleanup operations just before an object's memory is released by the GC, similar to a destructor in C++.
π Cleanup operations include properly closing database connections or ensuring data is written to a hard disk before the external resource pointers inside the object are destroyed.
π The GC thread calls the object's `finalize()` method only once immediately before sweeping out (destroying) the object.
Inheritance and Calling `finalize()`
π Every Java class is a descendant of the `Object` class, which contains a default, empty `finalize()` method.
π If a subclass does not override `finalize()`, the GC will call the version inherited from the `Object` class, which performs no action.
π Unlike C++, there is no automatic chaining of `finalize()` methods in Java; the subclass `finalize()` does not automatically call the superclass's `finalize()`βit must be called explicitly using `super.finalize()`.
Forced Execution and Reliability
β‘οΈ Developers can call `finalize()` manually like any other method (e.g., using `Runtime.getRuntime().runFinalization()`), but this does not guarantee the object's memory release; only the GC triggers memory release.
β‘οΈ Exceptions occurring inside the `finalize()` method are ignored by the Garbage Collector, ensuring the GC continues its process and destroys the object.
β‘οΈ Relying on forced execution is not recommended as it is not reliable; execution is only guaranteed when the GC runs.
Key Points & Insights
β‘οΈ `finalize()` serves as the object's "last wish" to execute critical closing/cleanup code before memory reclamation by the GC.
β‘οΈ If an object holds external resources (e.g., database connections), ensure the code to close these is placed within the `finalize()` method to prevent resource leaks.
β‘οΈ Do not depend on manual execution of `finalize()`; trust the Garbage Collector for invoking cleanup logic just before memory destruction.
πΈ Video summarized with SummaryTube.com on Dec 30, 2025, 14:53 UTC
Find relevant products on Amazon related to this video
As an Amazon Associate, we earn from qualifying purchases

Summarize youtube video with AI directly from any YouTube video page. Save Time.
Install our free Chrome extension. Get expert level summaries with one click.