ClassMemberImpliedModifier

Since Checkstyle 8.16

Description

Checks for implicit modifiers on nested types in classes and records.

This check is effectively the opposite of RedundantModifier. It checks the modifiers on nested types in classes and records, ensuring that certain modifiers are explicitly specified even though they are actually redundant.

Nested enums, interfaces, and records within a class are always static and as such the compiler does not require the static modifier. This check provides the ability to enforce that the static modifier is explicitly coded and not implicitly added by the compiler.

public final class Person {
  enum Age {  // violation
    CHILD, ADULT
  }
}
        

Rationale for this check: Nested enums, interfaces, and records are treated differently from nested classes as they are only allowed to be static. Developers should not need to remember this rule, and this check provides the means to enforce that the modifier is coded explicitly.

Properties

name description type default value since
violateImpliedStaticOnNestedEnum Control whether to enforce that static is explicitly coded on nested enums in classes and records. boolean true 8.16
violateImpliedStaticOnNestedInterface Control whether to enforce that static is explicitly coded on nested interfaces in classes and records. boolean true 8.16
violateImpliedStaticOnNestedRecord Control whether to enforce that static is explicitly coded on nested records in classes and records. boolean true 8.36

Examples

To configure the check so that it checks that all implicit modifiers on nested interfaces, enums, and records are explicitly specified in classes and records.

Configuration:

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

Code:

public final class Person {
  static interface Address1 {  // valid
  }

  interface Address2 {  // violation
  }

  static enum Age1 {  // valid
    CHILD, ADULT
  }

  enum Age2 {  // violation
    CHILD, ADULT
  }

  public static record GoodRecord() {} // valid
  public record BadRecord() {} // violation

  public static record OuterRecord() {
    static record InnerRecord1(){} // valid
    record InnerRecord2(){} // violation
  }
}
        

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.

Package

com.puppycrawl.tools.checkstyle.checks.modifier

Parent Module

TreeWalker