Differences between final, finally, and finalize in Java

Source: Internet
Author: User
Final It is used to declare attributes, methods, and classes, indicating that the attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited. FinallyIs the exception handling statement closed Is always executed. FinalizeIs a method of the object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, such as closing files.

 

Final-modifier (keyword) If Classes are declared as final, which means they cannot be assigned new child classes and cannot be inherited as parent classes. Therefore, a class cannot be declared both abstract and final. Declare a variable or method as final. Verify that they are not changed during use. Variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be overloaded.

Finally-provide finally for Exception Handling Block to perform any cleanup operation. If an exception is thrown, the matched catch clause is executed, and the control enters the Finally block (if any ).

Finalize-method name. Java technology allows you to use the finalize () method to clear objects from the memory before the Garbage Collector clears them. This method is implemented by the garbage collector when it determines that this object is not referenced. This object is called. It is defined in the object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object.

 

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.