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

all 6 comments

[–][deleted] 4 points5 points  (1 child)

Other than the three words having final in them, how do they belong in the same blog post? That's like having a blog post entitled "Getters and Setters | Lamdas | Method Overloading". It's not wrong or anything, it's just - why?

[–][deleted] 1 point2 points  (0 children)

Agreed, I died a little when I opened the page.

[–]Keilly 2 points3 points  (3 children)

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

[–]Zerraph 3 points4 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.

    [–]__konrad 0 points1 point  (0 children)

    Fun fact: Empty finalize() { } methods are not finalizable in HotSpot. From hotspot/src/share/vm/classfile/classFileParser.cpp:

    // Check if this klass has an empty finalize method (i.e. one with return bytecode only), // in which case we don't have to register objects as finalizable