RAII in Java… is resource disposal always so ugly?

后端 未结 5 836
日久生厌
日久生厌 2020-12-05 15:00

I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/f

5条回答
  •  盖世英雄少女心
    2020-12-05 15:50

    Yes, that's how java works. There is control inversion - the user of the object has to know how to clean up the object instead of the object itself cleaning up after itself. This unfortunately leads to a lot of cleanup code scattered throughout your java code.

    C# has the "using" keyword to automatically call Dispose when an object goes out of scope. Java has no such thing.

提交回复
热议问题