JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.CLASS.METH.NF : Method Should be final (Java)

Summary

A method should be final, since it is not private, not static, and not overridden in any subclass.

Properties

Class Name Method Should be final (Java)
Significance reliability
Mnemonic JAVA.CLASS.METH.NF
Categories
CWE CWE:493 Critical Public Variable Without Final Modifier
OWASP-2017 OWASP-2017:A1 Injection
OWASP-2021 OWASP-2021:A3 Injection
OWASP-2025 OWASP-2025:A05 Injection
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Method Should be final (Java)"

Example

// C.java 
public class C {
  private static int counter;
  private int id = getNextId();

  @Override
  public String toString() {                                // ok: overridden in Sub
      return "#" + getId();
  }

  protected int getId() {                    // "Method Should be final (Java)" warning issued here
      return id;
  }

  private int getNextId() {                                 // ok: private
    return counter++;
  }

  public static int getNumberOfInstances() {                // ok: static
      return counter;
  }
}
// Sub.java 
public class Sub extends C {

  @Override
  public String toString() {                 // "Method Should be final (Java)" warning issued here
    return "sub" + super.toString();
  }
}

In this example, the programmer should make those two methods final, as follows.

// after modification 
public class C {
  private static int counter;
  private int id = getNextId();

  @Override
  public String toString() {
      return "#" + getId();
  }

  protected final int getId() {
      return id;
  }

  private int getNextId() {
      return counter++;
  }

  public static int getNumberOfInstances() {
      return counter;
  }
}

public class Sub extends C {

  @Override
  public final String toString() {
      return "sub" + super.toString();
  }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.