1

Since Object#finalize()'s introduction in JDK 1.0, Java introduced 9 more versions.

Yet, despite its clear shortcomings, it is still available, even in JDK 10.

Oracle itself states in its documentation:

The finalize() Method

The Object class provides a callback method, finalize(), that may be invoked on an object when it becomes garbage. Object's implementation of finalize() does nothing—you can override finalize() to do cleanup, such as freeing resources.

The finalize() method may be called automatically by the system, but when it is called, or even if it is called, is uncertain. Therefore, you should not rely on this method to do your cleanup for you. For example, if you don't close file descriptors in your code after performing I/O and you expect finalize() to close them for you, you may run out of file descriptors.

Which begs the question: Are there any known proper uses of this method?

Very Objective
  • 601
  • 7
  • 16
  • 2
    `finalize()` has been _deprecated_ since Java 9, by the way, just not for removal (in response to "_...it is still available, even in JDK 10_"). It might never be removed to ensure they don't break anything. – Slaw Aug 12 '18 at 15:31
  • There is a thorough description in *Effective Java*. Google's internal practices page says of finalizers: 1) don't use them; 2) if you think you really need to use them, read the *Effective Java* item thoroughly. And then *still* don't use them. (There are cases where you do need them, e.g. interacting with native code. But the point is that you need them far, far less frequently than you might at first imagine). – Andy Turner Aug 12 '18 at 15:45
  • @Slaw what does *"deprecated but might never be removed"* mean? :) – WebViewer Aug 12 '18 at 15:59
  • 2
    @WebViewer When an item shouldn't be used for some reason it can be marked as "deprecated". In Java, this is done with the [`Deprecated`](https://docs.oracle.com/javase/10/docs/api/java/lang/Deprecated.html) annotation (Javadoc goes into more detail). However, just because an item has been deprecated doesn't mean it will be removed from the library. This is to ensure backwards compatibility; code compiled in Java 8 should run on Java 9. If backwards compatibility is not needed or desired then the item can be marked "deprecated for removal" which means it will be removed in a future version. – Slaw Aug 12 '18 at 16:08

1 Answers1

2

The only "meta usage" might be the example status of this API: although it is known and recommended to not be used, it wasn't removed. Or even deprecated until Java 9.

You may look at this simple method as evidence that the people responsible for the Java language are super reluctant to break backwards compatibility. Even for methods nobody should be using!

So, the actual answer is: no, there isn't. Of course, you could use the method to make experiments under which conditions it gets invoked, but well, you can't rely on the results of that research.

GhostCat
  • 137,827
  • 25
  • 176
  • 248