Skip to main content

The Signature of "finalize()" Should Match That of "Object.finalize()"

High
correctnessmaintainability

What is it?

This practice calls attention to incorrectly overloading the finalize method rather than properly overriding Object.finalize() in Java.

Why apply it?

Overloading Object.finalize() can introduce unexpected behavior, as the garbage collector does not invoke overloaded methods, leading to potential resource leaks. Correctly overriding it ensures predictability and proper resource management.

How to fix it?

Rename the method to a more descriptive name if it does not match Object.finalize(), effectively preventing misuse and maintaining clarity.

Examples

Example 1:

Negative

This example incorrectly overloads the finalize method, which the Garbage Collector does not recognize.

public class ResourceHandler {
private boolean isActive;

public int finalize(int resourceId) { // Noncompliant
// Logic to close the resource
isActive = false;
return resourceId;
}
}

Example 2:

Positive

This example correctly avoids overloading the finalize() method by using a descriptive method name.

public class ResourceHandler {
private boolean isActive;

public int closeResource(int resourceId) { // Compliant
// Logic to close the resource
isActive = false;
return resourceId;
}
}

Negative

An incorrect example where finalize() is overloaded instead of overridden, potentially leading to issues.

public class ConnectionManager {
private boolean isConnected;

public void finalize(String username) { // Noncompliant
// Disconnect logic here
isConnected = false;
}
}

Example 3:

Positive

A correct example illustrating a method named clearly, avoiding the problematic overloading of finalize().

public class ConnectionManager {
private boolean isConnected;

public void disconnectUser(String username) { // Compliant
// Disconnect logic here
isConnected = false;
}
}