Java Fundamentals strengthen 98:final, finally, finally difference

Source: Internet
Author: User

1. 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 both abstract and final.

Declaring variables or methods as final ensures that they are not changed in use. Its initialization can be in two places:

  The first is its definition , that is, to assign the value directly to the final variable definition;

The second is in the constructor . These two places can only be selected, either in the definition of the value, or in the constructor to give a value, not both in the definition of the value, but also in the constructor to give additional values, and in subsequent references can only read, not modifiable. A method that is declared final can also be used only and cannot be overridden (override).

2. Finally

Provides a finally block to perform any cleanup operations during exception handling. If an exception is thrown, the matching catch clause executes, and the control enters the finally block, if any. Use finally to maintain the internal state of an object and to clean up non-memory resources. If you don't have a finally, your code will be confusing.

3. Finalize

Finalize is 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.

Java Fundamentals strengthen 98:final, finally, finally difference

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.