Finalize () method

Source: Internet
Author: User
Finalize () method-general Linux technology-Linux programming and kernel information. The following is a detailed description. Some operations are required to cancel an object. For example, if an object is processing non-Java resources, such as a file handle or a window font, make sure that these resources are released before an object is revoked. To handle such a situation, Java provides a mechanism called finalization. You can use this mechanism to define some special operations that are executed when an object is to be released by the garbage collection program.

To add finalizer to a class, you only need to define the finalize () method. This method is called when Java recycles an object of this class. In the finalize () method, you must specify the operations that must be performed before an object is revoked. Garbage collection runs cyclically, and check that the object is no longer referenced by the running state or indirectly referenced by other objects. Before the object is released, the Java runtime system calls the finalize () method of the object.

The general format of the finalize () method is as follows:

Protected void finalize ()
{
// Finalization code here
}

The keyword protected is used to prevent code defined outside of this class from accessing the finalize () identifier. This identifier and other identifier will be explained in chapter 7th.

It is important to understand that finalize () is called before garbage collection. For example, if an object exceeds its scope, finalize () is not called. This means that you cannot know when -- or even whether -- finalize () is called. Therefore, your program should provide other methods to release the system resources used by the object, rather than relying on finalize () to complete normal operations of the program.

Note: If you are familiar with C ++, you know that C ++ allows you to define a destructor for a class, which is called before the object is out of scope. Java does not support this idea and does not provide undo functions. The finalize () method is only similar to the function of revoking a function. When you have rich experience with Java, you will see that Java uses the garbage collection subsystem and there is almost no need to use the Undo function.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.