You can overload the finalize
method, this would compile successfully :
public class Test
{
public void finalize(String hi)
{
System.out.println("test");
}
}
However it won't be called by the Garbage collector and JVM as it will call the one with no parameters.
GC and JVM will only and always call the finalize
with the signature
protected void finalize() throws Throwable
Workaround
You could simply override finalize
and make it 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);
}
}
That would print allo
. That way you could call finalize(String hi)
where you want, and the GC would also call it as you overrided finalize()
to call it.