Java 8 is this assertion about finalize method correct?

chiperortiz :

I been reading the book OCA Java SE 8 Programmer I Exam Guide by Kathy Sierra + Bert Bates.

There is something about the finalize method I don't get it quite well. It states on page 218:

Calling finalize() can actually result in saving an object from deletion.

Later on page 222:

You can make an object ineligible for GC from within finalize().

English is not my native language, but what I understand in both scenarios is that the finalize() method can prevent the object from being garbage collected? Is this so? Or am I misinterpreting it?

GhostCat salutes Monica C. :

This is really more of a theoretical thing: objects are eligible for garbage collection when they aren't referenced from other live objects anymore.

Thus: you could try to create such a reference within finalize(). Which would then theoretically prevent the object from being removed.

There is actually a "pattern name" for that: object resurrection. Now, if this is a pattern, or more of an anti pattern is open for debate.

(personally: I would never do that, and I have never been in a situation where it would have been necessary to use this pattern)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322675&siteId=1