finalize
rs should not be public SC-R1047It is generally recommended that you use interfaces such as AutoCloseable
to perform clean-up related tasks. However, if you choose to explicitly define a finalize
r, it is suggested that it have an access specifier that is more restrictive than public
such as protected
or private
. This signifies that it may not be programmatically be invoked.
class C {
override def finalize(): Unit = {
// clean up logic
}
}
class C {
override private def finalize(): Unit = {
// clean up logic
}
}
// Using the `AutoCloseable` interface to perform clean-up tasks
class C extends AutoCloseable {
override def close(): Unit = {
// clean up logic
}
}