Final,finally,finalize

Source: Internet
Author: User
Tags finally block

Final

Modifier (keyword) If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and declared final. Declaring variables or methods as final ensures that they are not changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified. The method that is declared final is also used only and cannot be overloaded.

Finally

Provides a finally block to perform any cleanup operations when exception handling occurs. If an exception is thrown, the matching catch clause executes, and the control enters the finally block, if any. General exception handling blocks are required.

Finalize

The method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector.

All classes in Java inherit the Finalize () method from the object class.

When the garbage collector (garbage Colector) decides to reclaim an object, it runs the Finalize () method of the object. It is worth the attention of C + + programmers that the Finalize () method is not equivalent to a destructor. There is no destructor in Java. A C + + destructor is run when the object dies. Because C + + does not have garbage collection, object space is recycled manually, so the programmer should delete () the object once it is not available. So in the destructor often do some file preservation and other finishing work. But unfortunately in Java, if the memory is always sufficient, then garbage collection may never happen, that is, filalize () may never be executed, and it is obvious that it is unreliable to expect it to do the finishing work.

So what exactly does finalize () do? Its main purpose is to reclaim the memory of special channel applications. Java programs have garbage collector, so in general memory problems don't bother programmers. But there is a jni (Java Native Interface) called Non-java Program (C or C + +), and finalize () works to reclaim this part of memory.

Final,finally,finalize

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.