Java Object finalize() Method
The Object finalize() method is used for the operation triggered when an instance is reclaimed by the garbage collector.
When the GC (garbage collector) determines that there are no more references to the object, the object's garbage collector calls this method.
Syntax
protected void finalize()
Parameters
-
None .
Return Value
No value is returned.
Example
The following example demonstrates the use of the finalize() method:
Example
import
java
.
util
.*;
class
RunoobTest
extends
GregorianCalendar
{
public
static
void
main
(
String
[
]
args
)
{
try
{
//
Create a RunoobTest object
RunoobTest
cal
=
new
RunoobTest
(
)
;
//
Output the current time
System
.
out
.
println
(
"
"
cal
.
getTime
(
)
)
;
//
finalize cal
System
.
out
.
println
(
"
Finalizing...
"
)
;
cal
.
finalize
(
)
;
System
.
out
.
println
(
"
Finalized.
"
)
;
}
catch
(
Throwable
ex
)
{
ex
.
printStackTrace
(
)
;
}
}
}
The execution result of the above program is:
Sun Oct 11 11:36:46 CST 2020
Finalizing...
Finalized.
Java Object Class