public class TestFinalize {
static int i = 0;
public TestFinalize() {
i++;
}
protected void finalize(){
i--;
}
public static void main(String[] args) {
TestFinalize testFinalize = new TestFinalize();
testFinalize = new TestFinalize();
System.gc();
System.out.println(i);
}
}
I expected that value of i will always be 1 in this case but when I ran this code multiple times it sometimes prints 1 and sometimes prints 2. Why is this happening?