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.