Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Object finalize() Method

Java Object finalize() Method

Java String ClassJava Object Class


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

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 String ClassJava Object Class