The Finalize () method in Java

Source: Internet
Author: User

The Finalize () method in the "Go" Java

This morning, see thinking in Java, "the fourth chapter initializes and clears". "Clear: End and garbage collection" When you see this thing.

Used to clean the drip ...

Of course, this method comes from Java.lang.Object

Rewrite of Finalize () method

Permissions (access) need to be protected or public, not private

The Finalize () method does not require a display call and is automatically called when garbage collection (GC) occurs.

According to my test, the garbage collection Method (System.GC ()) needs to be shown, and the presence of an anonymous object that has not been destroyed by new is required (calling other languages is not considered), and the Finalizze () method is called

Eg:

public class F {public   static void Main (String args[]) {      new F ();         int a[] = new Int[3];         F f= new F ();         System.GC ();     }     public void Finalize () throws throwable{                    //super.finalize ();           System.out.println ("Finalize method was called!");}     }

  

In the above code, a new anonymous object, an integer array, and an instance object F, respectively, are called by the Finalize method only when an anonymous object is created and the System.GC () method is called by the test result.

The Finalize () method in Java

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.