Is there a destructor for Java?

前端 未结 22 1588
[愿得一人]
[愿得一人] 2020-11-22 11:47

Is there a destructor for Java? I don\'t seem to be able to find any documentation on this. If there isn\'t, how can I achieve the same effect?

To make my question m

22条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 12:16

    The finalize() function is the destructor.

    However, it should not be normally used because it is invoked after the GC and you can't tell when that will happen (if ever).

    Moreover, it takes more than one GC to deallocate objects that have finalize().

    You should try to clean up in the logical places in your code using the try{...} finally{...} statements!

提交回复
热议问题