Java Final, Finally, Finalize

Final is a Keyword, final can be used in three different ways:

  1. final variable
  2. final method
  3. final class


  1. final variable 基本上就是 constant,一旦被赋值,就不能被变更;如果在class里declare a final variable, 那么在此class生成出的object就必须对这个 final variable赋值。
  2. final method 是method can't be over overridden.
  3. 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: 

  1. System.exit() is called in try block
  2. Another thread interrupts current one
  3. 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.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825063.html