The destructor method in Java Finalize
There is a concept of constructors and destructors in C + + programming, and a very important part of memory management technology, whereas in the Java language there is no concept of a destructor or destructor, except the concept of a constructor (also known as a constructor). This is because, in theory, the JVM is responsible for the destructor (destruction and recycling) of objects. The concept of garbage collection mentioned above. Does the Java language really not have a similar approach to the destructor function in C + +? In fact, the Finalize method in the Java language is very similar to the function of a destructor in the C + + language. The Finalize method is a method contained in the Java language base class object class, which is a method of protecting the type (protected), because all classes developed in the Java application are subclasses of the object. Therefore, the user class implicitly inherits the method from the object object. Therefore, we can call the Finalize method of its parent class in the Java class, and can overwrite the Finalize method inherited by itself. Although we can call the Finalize method of its parent class in a Java class, because the Finalize method does not automatically implement recursive calls, we must implement them manually, so the last statement of the Finalize function is usually the super.finalize () statement. In this way, we can implement the iterative invocation from bottom to top, that is, to release the resources of the user class itself, and then to release the resources of the parent class. Usually we can release some of the resources that are not easy to control and very important in the Finalize method, such as: some I/O operations, data connection. The release of these resources is critical to the entire application.
The Finalize method is ultimately invoked by the garbage collector in the JVM, and because the garbage collector calls Finalize time is uncertain or not timely, the timing of the call is not controllable for us, so sometimes we need to use other means to release the system resources that are occupied in the program. For example, you declare a destroy () method in your class, and in this method you add the processing code that frees the system resources, and you can use this destroy () method to release the system resources that are occupied by the members of the object when you have finished using the object. Although we can free up system resources by calling our own defined Destroy () method, it is advisable to put the call to the Destroy () method into the Finalize () method body of the current class, because it is safer and safer to do so. In the case of class depth inheritance, this approach is more efficient, and we can recursively invoke the Destroy method to release the resources that the parent class occupies when the subclass is destroyed, such as the following code:
1. Original base Class A
public class A {
Object a = null;
Public A () {
A = new Object ();
System.out.println ("Create a object");
}
protected void Destroy () {
System.out.println ("Release a object");
A = null;
Releasing the resources that are occupied by itself
...
}
protected void Finalize () throws java.lang.Throwable {
Destroy ();
Recursively call the Finalize method in a superclass
Super.finalize ();
}
}
2. Class A subclass B
public class B extends A {
Object B = null;
Public B () {
b = new Object ();
System.out.println ("Create B Object");
}
protected void Destroy () {
b = null;
Releasing the resources that are occupied by itself
System.out.println ("Release B Object");
Super.destroy ();
}
protected void Finalize () throws java.lang.Throwable {
Destroy ();
Recursively call the Finalize method in a superclass
Super.finalize ();
}
}
3. Class Two subclass C
public class C extends B {
Object c = null;
Public C () {
c = new Object ();
System.out.println ("Create C Object");
}
protected void Destroy () {
c = null;
Releasing the resources that are occupied by itself
System.out.println ("Release C Object");
Super.destroy ();
}
protected void Finalize () throws java.lang.Throwable {
Destroy ();
Recursively call the Finalize method in a superclass
Super.finalize ();
}
}
The inheritance relationship of the three classes above is very clear: A->b->c, Class A is the original base class (this is a customary term), Class B inherits Class A, and Class C inherits Class B. In fact, Class A is not really the original base class, we have already mentioned that the Java language of the original base class is the object class, although we do not have explicit declarations, but this is already a system of conventional.
To explain the problem in a simple and clear way, we have declared 3 methods in these three classes to illustrate the knowledge points explained above, we initialized an object A in the constructor of Class A, passed a = NULL in the Destroy method, and freed the resources that were occupied by itself. And in the Finalize method, we call the Destroy method to free the resources it occupies, and then call the Finalize method of its superclass object, which is the "double Insurance" memory release method we mentioned above, and the structure of Class B and Class C are very similar to Class A. In addition to releasing the resources they occupy, they also invoke the Destroy method and the Finalize method of their superclass in their corresponding methods to free the resources occupied by the superclass. such as the Destroy method of calling its superclass a in class B and the Finalize method and the Destroy method and the Finalize method that call its superclass B in Class C. However, Class A differs from Class B and Class C in that there is no Super.destroy () statement in its destroy method because its superclass object does not have a destroy method. Let's take a look at what happens when we call Initialize and Destroy Class C. The following is the source code for the test class testing that completes this procedure:
public class Test {
c = null;
Public Test () {
c = new C ();
}
public static void Main (String args[]) {
MyClass me = new MyClass ();
Me.destroy ();
}
protected void Destroy () {
if (c!= null) {
C.destroy ();
}else {
System.out.println ("C object has been released");
}
}
}
Compile execution Test.java:
> Javac Test.java
> Java Test
The following is the result of the operation of this program:
Create a object
Create a B Object
Create a C object
Release C Object
Release B Object
Release a object
We notice that when you initialize the object of Class C in the test class, its constructors produce recursive calls, which are invoked and initialized by the base class, and the system also produces recursive calls when the Destroy method of the Class C object is invoked, but the order of the calls is the exact opposite of the order in which the call was initialized. The invocation order of the freed resource is initiated by a subclass, and the resource release method Destroy () of its superclass is called in turn. Thus, we should try to avoid creating and initializing a large number of objects in the default constructor of the class as much as possible when designing a class. One reason is that there is a large resource overhead in the case of instantiating itself, and another reason is that its subclasses also incur significant system resource overhead when instantiated. Because even if we don't have the constructor that wants to invoke the parent class to create a lot of useless objects (at least sometimes they don't make sense to us), the system creates them automatically, and these operations and processes are implicit to us. In order to prevent this from happening, causing unnecessary waste of memory resources, we should try not to create, initialize large numbers of objects in the constructor of the class or perform some complex, time-consuming operation logic.