UnusedPrivateField

Since Checkstyle 14.1.0

Description

Check that a private field is declared, but never used. Fields with any other visibility (package-private, protected, or public), including those declared in an implicitly declared class (compact source file), are not checked.

Properties

name description type default value since
ignoreAnnotationCanonicalNames Specify annotations canonical names which ignore variables in consideration. String[] java.io.Serial 14.1.0
ignoredFieldPattern Specify a regular expression pattern for field names to ignore, even if they otherwise satisfy this check's detection of an unused private field. Note this replaces the default value entirely — to keep serialVersionUID ignored alongside your own pattern, include it explicitly, e.g. ^(serialVersionUID|LOG|LOGGER)$. Pattern serialVersionUID 14.1.0

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="UnusedPrivateField"/>
  </module>
</module>

Example:


import java.io.Serial;

public class Example1 {

  @SuppressWarnings("unused")
  private String _id; // violation 'Unused private field'

  @interface Getter {}

  private int unused; // violation 'Unused private field'

  private int used;

  @Serial
  private static final long serialVersionUID = 1L;

  @Getter
  private int getter; // violation 'Unused private field'

  private int getUsed(){
    return used;
  }

  private static final int CONSTANT = 10; // violation 'Unused private field'

  void foo() {
    System.out.println(used); // ok, used in inner class
  }
}

To configure the check to allow UnusedPrivateField with annotation:


<module name="Checker">
  <module name="TreeWalker">
    <module name="UnusedPrivateField">
      <property name="ignoreAnnotationCanonicalNames"
                value="Getter,Serial,SuppressWarnings"/>
    </module>
  </module>
</module>

Example:


import java.io.Serial;

public class Example2 {

  @SuppressWarnings("unused")
  private String _id;

  @interface Getter {}

  private int unused; // violation 'Unused private field'

  private int used;

  @Serial
  private static final long serialVersionUID = 1L;

  @Getter
  private int getter; // ok, as is supressed by property

  private int getUsed(){
    return used;
  }

  private static final int CONSTANT = 10; // violation 'Unused private field'

  void foo() {
    System.out.println(used); // ok, used in inner class
  }
}

To configure the check to allow UnusedPrivateField with FieldNames:


<module name="Checker">
  <module name="TreeWalker">
    <module name="UnusedPrivateField">
      <property name="ignoredFieldPattern"
        value="serialVersionUID|getter|_id|unused"/>
    </module>
  </module>
</module>

Example:


import java.io.Serial;

public class Example3 {

  @SuppressWarnings("unused")
  private String _id;

  @interface Getter {}

  private int unused;

  private int used;

  @Serial
  private static final long serialVersionUID = 1L;

  @Getter
  private int getter; // ok, as is supressed by property

  private int getUsed(){
    return used;
  }

  private static final int CONSTANT = 10; // violation 'Unused private field'

  void foo() {
    System.out.println(used); // ok, used in inner class
  }
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.coding.UnusedPrivateFieldCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker