4

I've read somewhere that every method can be overloaded.

finalize()

is a method of course. But while searching I also found that you cannot overload this method.

So the Question is Can we overload the finalize() method? If yes then how is it implemented?

8
  • I think the question is about finalize(), not final. Commented Apr 21, 2015 at 13:45
  • How can it be overloaded if it doesn't accept any arguments ? or did you mean overrided ? why don't you simply try to do it and see what happens ? Commented Apr 21, 2015 at 13:45
  • 3
    @alfasin For the record, though, a method that doesn't accept any arguments can easily be overloaded -- just write a method of the same name, in the same class, that does take arguments. Commented Apr 21, 2015 at 13:46
  • 2
    First things first: why use finalize() at all? Commented Apr 21, 2015 at 13:47
  • 1
    "while searching I also found that you cannot overload this method" <-- source, please? Commented Apr 21, 2015 at 13:49

5 Answers 5

7

Yes, you can. But the overloaded version won't be called by the JVM.

@Override protected void finalize() throws Throwable { super.finalize(); } protected void finalize(String lol) throws Throwable { finalize(); } 
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

Method Overloading is done in same & child class, while Overriding is done in child class only. We do override other Object class methods (equals(), hashCode() etc.) due to their significance, but never overload any of these methods.

So while you CAN overload the finalize() method, it would be just like a new method in your child class, and wouldn't be considered by JVM (as a finalize() method really).

1 Comment

a method overloading can be done in a class that inherits from a base class. As for instance the finalize method is protected and defined on the Object class, therefore if I define any other method called finalize with a different signature from the base finalize method in any java class I have indeed overloaded the base finalize method
1

You can overload the finalize method without any issue. Here is another example:

public class FinalizeOverloader { protected void finalize(String userId) { System.out.println("I could overload finalize..."); } public static void main(String[] args) { FinalizeOverloader fO = new FinalizeOverloader(); fO.finalize("AbC"); } } 

Comments

0

While overloading finalize() is possible just like any other method, I don't see the point to do that. JVM will not invoke that method and even if this is not your goal, overloading it would just generate confusion.

If you meant overriding on the other hand, it could make more sense but it is strongly a not recommended approach. There are many downsides of using finalizers. For more info you can find some good advice (Item 7) in Joshua Bloch's "Effective Java" book. It basically says, avoid using finalizers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.