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:
- A
final Listcan still get new elements. finallyruns even whentryhas areturn.- Don't
returninfinally: 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.