The JVM will ignore a trivial finalize() method ... Otherwise, if an
instance is being created, and that instance has a non-trivial
finalize() method defined or inherited, then the JVM will do the
following:
The JVM will create the instance
The JVM will also create an instance of the java.lang.ref.Finalizer class, pointing to that object instance just created (and pointing to
a queue that it will be put on by the GC)
The java.lang.ref.Finalizer class holds on to the java.lang.ref.Finalizer instance that was just created (so that it is
kept alive, otherwise nothing would keep it alive and it would be
GCed at the next GC).
Called by the garbage collector on an object (that object may be any object that is why it's declared in the object class and not in the finalizer class) when garbage collection
determines that there are no more references to the object.
# 1 楼答案
“Finalizer”类java.lang.ref.Finalizer和“Finalizer()”方法java.object.finalize是两个独立的、不同的东西
正如alexey提到的“finalize()”,
所以要回答你的问题:“finalize()”是你要做的事情(在GC期间定制对象的清理行为);“Finalize”是JVM创建的一个对象(用于促进和管理清理)
出于许多原因,包括performance和security,创建自己的定制终结器通常是一个糟糕的想法,除非你必须这样做
# 2 楼答案
关于方法
finalize()
请查看javadoc可以在任何类上重写该方法,以指定垃圾收集器收集给定对象时要执行的操作