I have a java class which has allocated resources that must be closed. I don't really know how to enforce this, my current approach is implementing the interface AutoCloseable
, hoping that the caller calls close()
sometimes and adding a boolean
closed which is checked at the begin of every method and if true
, the method throws IllegalStateException
.
Both, that the caller has to call close()
and that I have to check closed at the beginning of every method aren't really nice, is there some best practice to avoid this?