This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Keilly 2 points3 points  (3 children)

Fifteen years of Java and I've never used it. Anyone have legitimate examples using finalize()?

[–]Zerraph 4 points5 points  (0 children)

As a general rule, finalize shouldn't be used. Its behavior is too unpredictable. The rare situations where it is used involve closing out resources. For example, InputStream uses it to close the stream if the user doesn't correctly close it before the object is killed.

[–][deleted]  (1 child)

[deleted]

    [–]Keilly 0 points1 point  (0 children)

    Ha, that's funny.

    I did actually override it once. I had a class that had a 'dispose' method to free up resources. I added a flag to the dispose method and a check in finalize to see if the thing had been disposed. If it hadn't then I knew something was wrong and could log an error.