SteelReed

Search

Search posts, questions…

Java CoreQuestion 17 / 43

Junior

What is the difference between final, finally and finalize? Does finally always run?

Short answer

Three things that only share a name: final blocks changes, finally is a cleanup block that always runs after try, finalize() is an old method that has been removed from use.

What it is Remember
final keyword locks the reference only, the object is not immutable
finally block after try always runs, except System.exit, a killed JVM, a hung thread
finalize() method called by GC deprecated; use try-with-resources, Cleaner

3 points to say:

  1. A final List can still get new elements.
  2. finally runs even when try has a return.
  3. Don't return in finally: it swallows the original error.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.