Analysis on the difference of final,finally,finalize _java

Source: Internet
Author: User
Tags exception handling modifier stub

1.final
The final modifier class, which shows that the class cannot be inherited, is a top-level class.
Final modifier variable, indicating that the variable is a constant.
The final modification means that this method cannot be overridden, but can be dashed in the final method.

For example, there is a base class person with a public final void eat () method that can overload the same method in the person class, such as public void eat (String name,int age). If there is a subclass student, then the student method of the parent class can be override, but not the final method override.

Person

Copy Code code as follows:

Package test2;

public class Person {
private String name;
private int age;

Public final void Eat ()
{
System.out.println ("This are in person class");
}

public void Eat (String name,int age)
{

}

}




Student
Copy Code code as follows:

Package test2;

public class Student extends person {

@Override
public void Eat (String name, int age) {
TODO auto-generated Method Stub
Super.eat (name, age);
}
}




a common final method is the wait () and notify () method in the object class.

2.finally
Finally is the keyword, in exception handling, the content that needs to be run is executed in the TRY clause, the catch clause is used to catch the exception, and the finally clause indicates that it executes regardless of whether an exception occurred. Finally dispensable. But Try...catch must appear in pairs.

3.finalize ()
Finalize () method name, method of object class, Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector clears the object out of memory. This method is invoked by the garbage collector when determining that the object is not referenced. The Finalize () method overwrites the Finalize () method on the subclass of the object invocation before the garbage collector deletes the object to defragment the system resources or perform other cleanup operations.

Code instance:

Copy Code code as follows:



Class Person


{


private String name;


private int age;

Public person (String name, int age) {
THIS.name = name;
This.age = age;
}

Public String toString ()
{
Return "Name:" +this.name+ ", Age:" +this.age;
}

public void Finalize () throws throwable{//object free space is the default call to this method
System.out.println ("The object is freed-->" +this);//Output the secondary object directly, invoke the ToString () method
}

}

public class Systemdemo {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Person Per=new person ("Zhangsan", 30);
per=null;//disconnect references, freeing space
Method 1:
System.GC ()//Mandatory free space
Method 2:
Runtime Run=runtime.getruntime ();
RUN.GC ();
}

}

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.