Wednesday, October 21, 2009

Final vs Finally vs Finalize

A short note for myself:

Final: used for defining a class non-subclassable, and making a member variable as a
constant which cannot be modified, and declare a method non-overridable.

Finally: to provide the capability to execute code no matter whether or
not an exception is thrown or caught in the try-catch exception handler.

Finalize: helps in garbage collection to clean up objects that are no longer required. Usualy Java will do the garbage collection automatically.

2 comments:

  1. Just a note on finalize. It was designed to free resources (like a connection pool), but people really shouldn't use it. There are huge performance issues (since there is a method to be called before collection) and you don't know when it's gonna be called (maybe it is to late for your resource).

    ReplyDelete
  2. Yupe, I agree on that. I don't use finalize in my codes because I'll just leave it to the JVM to do the garbage collection. :)

    ReplyDelete