How do you write a deconstructor in Java?

后端 未结 7 2099
萌比男神i
萌比男神i 2020-12-20 08:17

I came across this question and am looking for some ideas?

7条回答
  •  温柔的废话
    2020-12-20 08:49

    Try-with-resources is available as of Java 1.7

    Anything that inherits from Closeable or Autocloseable can use it.

    https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

    try (FileReader br = new FileReader(path)) {
        return br.readLine();
    }
    

    This will automatically call a close function which is guaranteed to be called at the end of the block.

提交回复
热议问题