Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any need for java's finalize method?

Tags:

java

finalize

Everything that I have read about the java finalize method says NOT to use it. It seems that it is almost never guaranteed to be called and may present problems even when it is.

There are a few other questions that ask when to use it, and it seems the general consensus is NEVER.

I have never used it myself (mainly because of the warnings not to), and I haven't seen it used anywhere.

Are there any cases at all where it would be appropriate? Are there cases where there is no alternative?

If not, why is it there? Are there internal classes that do use it and require the method to be available? Or is it just something that shouldn't be there?

I'm not so much interested in when I should use it (which has been answered with "never"), but clarification on why it is even there given that answer of "never". If it is so useless and dangerous, why hasn't it been depreciated and removed?

like image 396
Matthew Avatar asked Feb 27 '16 06:02

Matthew


1 Answers

Backwards compatibility. Someone first thought it would be a good idea, and then when the world realized it was not such a great idea, it was too late.

These things are barely ever removed. Java is full of concepts that are considered bad idea nowadays but haven't been removed nevertheless - some more examples that come to my mind are clone() or Thread.stop().

like image 116
Jiri Tousek Avatar answered Sep 30 '22 17:09

Jiri Tousek