1.final
The final modified class indicates that this class cannot be inherited and is a top-level class.
final modifies a variable to indicate that the variable is a constant.
The final modified method means that this method cannot be overridden, but it can be overridden in the final method.
For example, there is a base class Person with a public final void eat() method. You can overload the method with the same name in the Person class, such as public void eat(String name, int age). If there is a subclass Student, then the non-final method of the parent class can be overridden in Student, but the final method cannot be overridden.
Person
Copy the code code as follows:
package test2;
public class Person {
private String name;
private int age;
public final void eat()
{
System.out.println("this is in person class");
}
public void eat(String name,int age)
{
}
}
Student
Copy the 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);
}
}
Common final methods are the wait() and notify() methods in the Object class.
2.finally
finally is the keyword. In exception handling, the try clause executes what needs to be run. The catch clause is used to catch exceptions. The finally clause means that it will be executed regardless of whether an exception occurs. finally is optional. But try...catch must appear in pairs.
3.finalize()
finalize() method name, method of Object class, Java technology allows the use of the finalize() method to do necessary cleanup work before the garbage collector clears the object from memory. This method is called by the garbage collector on this object when it determines that the object is not referenced. The finalize() method is to override the finalize() method called by the subclass of this object before the garbage collector deletes the object to organize system resources or perform other cleanup operations.
Code example:
Copy the 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{//This method is called by default when the object releases space
System.out.println("Object is released-->"+this);//Output the secondary object directly and call 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 and release space
//Method 1:
System.gc();//Forcibly release space
//Method 2:
// Runtime run=Runtime.getRuntime();
// run.gc();
}
}