Avoid useless final type in interface method

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: java-code-style/final-param-in-abstract-method

Language: Java

Severity: Notice

Category: Code Style

Description

The rule “Avoid useless final type in interface method” advises against the unnecessary use of the final keyword in the method parameters of an interface. In Java, the final keyword is used to denote that a variable cannot be changed once assigned. However, in the context of an interface method, this is redundant as the value of the parameter cannot be changed within the method anyway.

The importance of this rule lies in the clarity and simplicity of code. Unnecessary use of final in this context can lead to confusion for those reading the code, as it suggests that there may be a specific reason for its use when there is not. It can also clutter the code, making it less readable.

Good coding practices to avoid this rule violation include simply not using the final keyword in the method parameters of an interface. This does not affect the functionality of the code, but it makes it cleaner and easier to understand. For example, instead of writing void process(final Object arg);, you can write void process(Object arg);. This maintains the same functionality but improves the readability of the code.

Non-Compliant Code Examples

public interface FooInterface {
  void process(final Object arg); // Avoid using final here
}

Compliant Code Examples

public interface FooInterface {
  void process(Object arg);
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis