What is the best way to clean up an Object in Java?

后端 未结 9 1921
悲&欢浪女
悲&欢浪女 2021-01-01 14:05

We don\'t have any destructor in Java as we have in C++.

Q1. How should we clean up any Object in java.

Q2. Is there any a

9条回答
  •  被撕碎了的回忆
    2021-01-01 14:54

    The best way to clean up after objects is to just drop the object.

    Finally blocks can be abstracted using the Execute Around idiom.

    Finalisers should be avoided. They probably wont get called immediately. The clean up should happen anyway. They are relatively slow. You might want to add one as a safety-net if you are writing a low-level resource wrapper (for, say, a file handle) if performance is not critical.

提交回复
热议问题