Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
JerryWang
Employee
Employee
0 Kudos


There is a method finalized defined on Object:


In order to test this method, I wrote the following test code:




package basic;
public class finalizeTest {
private String name;
public void finalize(){
System.out.println("finalize called: " + this.name);
System.out.println("Thread id in finalize: " + Thread.currentThread().getId());
}
public finalizeTest(String name){
this.name = name;
}
public static void main(String[] args) {
Object object = null;
System.out.println("Thread id: " + Thread.currentThread().getId());
finalizeTest test = new finalizeTest("Jerry");
new finalizeTest("Scala");
System.gc();
}
}

The code gives the following output:




Thread id: 1
finalize called: Scala
Thread id in finalize: 3

I have learned two points through this test program:


1. the finalize method redefined in my own class is called in a different thread:




2. The object instance with name "Scala" is considered by JVM as "no more reference has pointed to it", so it is decided by JVM to call finalize method redefined on the class when GC method is called explicitly.