Always call super.finalize
explicitly. This ensures that cleanup steps specified by the parent class are not skipped.
1public class FinalizeOnSuperClassNoncompliant {
2 protected abstract class FeedParser {
3 @Override
4 protected abstract void finalize() throws IOException;
5 }
6
7 protected abstract class ETLFeedParser extends FeedParser {
8 private BufferedReader feedReader;
9 // Noncompliant: does not call super.finalize().
10 @Override
11 protected void finalize() throws IOException {
12 feedReader.close();
13 }
14 }
15}
1public class FinalizeOnSuperClassCompliant {
2 protected abstract class FeedParser {
3 @Override
4 protected void finalize() throws IOException {
5 System.out.println("finalize-class");
6 }
7 }
8
9 protected abstract class ETLFeedParserCompliant extends FeedParser {
10 private BufferedReader feedReader;
11 @Override
12 // Compliant: calls super.finalize() explicitly.
13 protected void finalize() throws IOException {
14 try {
15 feedReader.close();
16 }
17 finally {
18 super.finalize();
19 }
20 }
21 }
22}