Constructors
- ✓public Object()
Methods
- ✓protected java.lang.Object clone() throws java.lang.CloneNotSupportedException
- ✓public boolean equals(java.lang.Object arg0)
- ✗@Deprecated(since="9"forRemoval=true)
protected void finalize() throws java.lang.ThrowableComparing jdk-20-ga and jdk-21+35finalize
Deprecated, for removal: This API element is subject to removal in a future version.Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides thefinalize
method to dispose of system resources or to perform other cleanup.When running in a Java virtual machine in which finalization has been disabled or removed, the garbage collector will never call
finalize()
. In a Java virtual machine in which finalization is enabled, the garbage collector might callfinalize
only after an indefinite delay.The general contract of
finalize
is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. Thefinalize
method may take any action, including making this object available again to other threads; the usual purpose offinalize
, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.The
finalize
method of classObject
performs no special action; it simply returns normally. Subclasses ofObject
may override this definition.The Java programming language does not guarantee which thread will invoke the
finalize
method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.After the
finalize
method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.The
finalize
method is never invoked more than once by a Java virtual machine for any given object.Any exception thrown by the
finalize
method causes the finalization of this object to be halted, but is otherwise ignored.- API Note:
- Classes that embed non-heap resources have many options for cleanup of those resources. The class must ensure that the lifetime of each instance is longer than that of any resource it embeds.
Reference.reachabilityFence(java.lang.Object)
can be used to ensure that objects remain reachable while resources embedded in the object are in use.A subclass should avoid overriding the
finalize
method unless the subclass embeds non-heap resources that must be cleaned up before the instance is collected. Finalizer invocations are not automatically chained, unlike constructors. If a subclass overridesfinalize
it must invoke the superclass finalizer explicitly. To guard against exceptions prematurely terminating the finalize chain, the subclass should use atry-finally
block to ensuresuper.finalize()
is always invoked. For example,@Override protected void finalize() throws Throwable { try { ... // cleanup subclass state } finally { super.finalize(); } }
- Throws:
Throwable
- theException
raised by this method- See Java Language Specification:
- Moved out of a link with destination https://docs.oracle.com/javase/specs/jls/se20/html/jls-12.html#jls-12.6.Moved to a link with destination https://docs.oracle.com/javase/specs/jls/se21/html/jls-12.html#jls-12.6. Moved out of a link with destination https://docs.oracle.com/javase/specs/jls/se20/html/jls-12.html#jls-12.6.Moved to a link with destination https://docs.oracle.com/javase/specs/jls/se21/html/jls-12.html#jls-12.6.12.6 Finalization of Class Instances
- See Also:
- ✓public final java.lang.Class<?> getClass()
- ✓public int hashCode()
- ✓public final void notify()
- ✓public final void notifyAll()
- ✓public java.lang.String toString()
- ✓public final void wait() throws java.lang.InterruptedException
- ✓public final void wait(long arg0) throws java.lang.InterruptedException
- ✓public final void wait(long arg0, int arg1) throws java.lang.InterruptedException
Summary
Elements | Comments | Descriptions | Total | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed | ||
finalize() | 1 | 1 | ||||||||
Total | 1 | 1 |
Subclasses that override
finalize
to perform cleanup should use alternative cleanup mechanisms and remove thefinalize
method. UseCleaner
andPhantomReference
as safer ways to release resources when an object becomes unreachable. Alternatively, add aclose
method to explicitly release resources, and implementAutoCloseable
to enable use of thetry
-with-resources statement.This method will remain in place until finalizers have been removed from most existing code.