You can overload the finalize () method in Java
•
Java
I've read a method, and each method can be overloaded
It's a method, of course But when searching, I also found that you can't overload this method
So the question is, can we overload the finalize () method? If so, how is it implemented?
Solution
You can overload the finalize method, which will compile successfully:
public class Test { public void finalize(String hi) { System.out.println("test"); } }
But the garbage collector and JVM will not call it because it will call the one without parameters
GC and JVM will always call finalize with signature
protected void finalize() throws Throwable
resolvent
You can simply override finalize and call the method with parameters:
public class Test { public static void main(String args[]) { new test(); System.gc(); } @Override public void finalize() throws Throwable { super.finalize(); this.finalize("allo"); } public void finalize(String hi) { System.out.println(hi); } }
This will print allo In this way, you can call finalize (string HI) where you want. When you override finalize () to call it, GC will call it
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码