如何定义析构函数?
How to define destructors?
public class A {
double wage;
A(double wage){
this.wage=wage;
}
}
//在这段代码中,我应该定义构造函数和析构函数。
- 定义析构函数的代码是什么?
编写自己的方法并使用它。不建议覆盖 finalize 方法。
在 Java 中有 no destructors
,但您可以使用方法 Object#finalize() 作为变通方法。
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is guaranteed,
however, that the thread that invokes finalize will not be holding any
user-visible synchronization locks when finalize is invoked. If an
uncaught exception is thrown by the finalize method, the exception is
ignored and finalization of that object terminates.
class Book {
@Override
public void finalize() {
System.out.println("Book instance is getting destroyed");
}
}
class Demo {
public static void main(String[] args) {
new Book();//note, its not referred by variable
System.gc();//gc, won't run for such tiny object so forced clean-up
}
}
输出:
Book instance is getting destroyed
Runs the garbage collector. Calling the gc method suggests that the
Java Virtual Machine expend effort toward recycling unused objects in
order to make the memory they currently occupy available for quick
reuse. When control returns from the method call, the Java Virtual
Machine has made a best effort to reclaim space from all discarded
objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object. A subclass
overrides the finalize method to dispose of system resources or to
perform other cleanup.
public class A {
double wage;
A(double wage){
this.wage=wage;
}
}
//在这段代码中,我应该定义构造函数和析构函数。
- 定义析构函数的代码是什么?
编写自己的方法并使用它。不建议覆盖 finalize 方法。
在 Java 中有 no destructors
,但您可以使用方法 Object#finalize() 作为变通方法。
The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
class Book {
@Override
public void finalize() {
System.out.println("Book instance is getting destroyed");
}
}
class Demo {
public static void main(String[] args) {
new Book();//note, its not referred by variable
System.gc();//gc, won't run for such tiny object so forced clean-up
}
}
输出:
Book instance is getting destroyed
Runs the garbage collector. Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.