This rule raises an issue when a private method is never referenced in the code.

Why is this an issue?

A method that is never called is dead code, and should be removed. Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.

This rule detects methods that are never referenced from inside a translation unit, and cannot be referenced from the outside.

Code examples

Noncompliant code example

public class Foo implements Serializable
{
  public static void doSomething() {
    Foo foo = new Foo();
    ...
  }

  private void unusedPrivateMethod() {...}
  private void writeObject(ObjectOutputStream s) {...}  //Compliant, relates to the java serialization mechanism
  private void readObject(ObjectInputStream in) {...}  //Compliant, relates to the java serialization mechanism
}

Compliant solution

public class Foo implements Serializable
{
  public static void doSomething(){
    Foo foo = new Foo();
    ...
  }

  private void writeObject(ObjectOutputStream s) {...}  //Compliant, relates to the java serialization mechanism
  private void readObject(ObjectInputStream in) {...}  //Compliant, relates to the java serialization mechanism
}

Exceptions

This rule doesn’t raise issues for:

The rule does not take reflection into account, which means that issues will be raised on private methods that are only accessed using the reflection API.