+ 1

Guys I am kinda confused can anyone Explain

Well this is the code https://code.sololearn.com/cAnoP85svs4U code: class First{ int x; public First(int x){ this.x = x; } protected void finalize() throws Throwable{ System.out.println("Well this apparently a finalized statement + " + x); } } public class Program { public static void main(String[] args) { First f1 = new First(100); First f2 = new First(200); f1 = f2; System.gc(); System.out.println("Hello World"); } } well It doesn't do what I intended to do guys pls help me understand also Output: Hello World Note: ./Playground/First.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

28th Apr 2022, 5:32 PM
Azimul Hasan
Azimul Hasan - avatar
2 Answers
+ 3
Finalize is deprecated since Java 9. You should not use it. More information: https://stackoverflow.com/questions/56139760/why-is-the-finalize-method-deprecated-in-java-9
28th Apr 2022, 5:35 PM
Tibor Santa
Tibor Santa - avatar
+ 2
As to another point, it is bad practice to use System.gc() at all. And there is no guarantee that garbage collection would start immediately. So by the time your objects are cleared from memory, your program will have finished already. https://www.baeldung.com/java-system-gc
28th Apr 2022, 5:39 PM
Tibor Santa
Tibor Santa - avatar