Java, executing a method when object's scope ends

后端 未结 5 1244
庸人自扰
庸人自扰 2021-02-12 20:48

I\'ve an object with a certain state. The object is passed around and it\'s state is temporarly altered. Something like:

public void doSomething(MyObject obj) {
         


        
5条回答
  •  -上瘾入骨i
    2021-02-12 21:07

    Java 7 has try-with-resources and you can extend AutoCloseable class to make use of it.

    For Java 6 you may want to see the related question: What is the best way to emulate try-with-resources in Java 6?

提交回复
热议问题