Scala

Scala

Made by DeepSource

Explicitly defined finalizers should not be public SC-R1047

Bug risk
Major

It is generally recommended that you use interfaces such as AutoCloseable to perform clean-up related tasks. However, if you choose to explicitly define a finalizer, 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.

Bad practice

class C {
  override def finalize(): Unit = {
    // clean up logic
  }
}

Recommended

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
  }
}