Analysis of the difference between final, finally, finalize

1. The
final modified class, indicating that this class cannot be inherited, is a top class.
The final modification variable indicates that this variable is a constant.
The final modification method means that this method cannot be overridden, but it can be used in the final method.

For example, there is a base class Person, which has a public final void eat() method. You can override the method of the same name in the Person class, such as public void eat(String name,int age). If there is a subclass of Student, then the non-final method of the parent class can be overridden in the Student, but the final method cannot be overridden.

Person


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


package test2;
public class Student extends Person {
    @Override
    public void eat(String name, int age) {
        // TODO Auto-generated method stub
        super.eat(name, age);
    }
}

The common final methods are the wait() and notify() methods in the Object class.

2. Finally
finally is a 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 means that it will be executed regardless of whether an exception occurs. Finally dispensable. But try...catch must appear in pairs.

3.finalize()
finalize() method name, method of Object class, Java technology allows the use of finalize() method to do the necessary clean-up work before the garbage collector clears the object from memory. This method is called by the garbage collector when it determines that the object is not referenced. The finalize() method is to override the finalize() method of the subclass called on the object before the garbage collector deletes the object to clean up system resources or perform other cleanup operations.
Code example:


class Person
{
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString()
    {
        return "姓名:"+this.name+",年龄:"+this.age;
    }

    public void finalize() throws Throwable{//对象释放空间是默认调用此方法
        System.out.println("对象被释放-->"+this);//直接输出次对象,调用toString()方法
    }

}
public class SystemDemo {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person per=new Person("zhangsan",30);
        per=null;//断开引用,释放空间
        //方法1:
        System.gc();//强制性释放空间
        //方法2:
//        Runtime run=Runtime.getRuntime();
//        run.gc();
    }
}

The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope everyone will support you

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114437963