Java Final, Finally, Finalize
Final is a Keyword, final can be used in three different ways:
- final variable
- final method
- final class
- final variable 基本上就是 constant,一旦被赋值,就不能被变更;如果在class里declare a final variable, 那么在此class生成出的object就必须对这个 final variable赋值。
- final method 是method can't be over overridden.
- final class is a class can't be inherited.
Finally is used in try/catch block, it is guaranteed to be executed no matter exception is thrown or not.
Circumstances that finally block are NOT executed:
- System.exit() is called in try block
- Another thread interrupts current one
- JVM crashes first
Fianllize is a method. finalize() is called by Garbage collection thread just before collecting eligible objects.
It is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步