Java, executing a method when object's scope ends

后端 未结 5 1241
庸人自扰
庸人自扰 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条回答
  •  伪装坚强ぢ
    2021-02-12 21:07

    No, there's nothing like that. The closest you've got is try/finally.

    In C# there's the using statement, which executes a Dispose method at the end:

    using (Stream x = ...)
    {
    } // x.Dispose() is called here, in a finally block
    

    There's a possibility that Java 7 will gain something a bit like this, but I don't think anything's been set in stone yet.

提交回复
热议问题