java - implements Closeable or implements AutoCloseable - Stack Overflow
Closeable
extends AutoCloseable
, and is specifically dedicated to IO streams: it throws IOException instead of Exception, and is idempotent, whereas AutoCloseable doesn't provide this guarantee.
This is all explained in the javadoc of both interfaces.
Implementing AutoCloseable (or Closeable) allows a class to be used as a resource of the try-with-resources construct introduced in Java 7, which allows closing such resources automatically at the end of a block, without having to add a finally block which closes the resource explicitely.
Your class doesn't represent a closeable resource, and there's absolutely no point in implementing this interface: an IOTest can't be closed. It shouldn't even be possible to instanciate it, since it doesn't have any instance method. Remember that implementing an interface means that thee is a is-a relationship between the class and the interface. You have no such relationship here.
Read full article from java - implements Closeable or implements AutoCloseable - Stack Overflow
No comments:
Post a Comment