Since Checkstyle 8.16
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.
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 |
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 Example1 { static interface Address1 { } interface Address2 { // violation, 'Implied modifier 'static' should be explicit' } static enum Age1 { CHILD, ADULT } enum Age2 { // violation, 'Implied modifier 'static' should be explicit' CHILD, ADULT } public static record GoodRecord() {} // violation below, 'Implied modifier 'static' should be explicit' public record BadRecord() {} public static record OuterRecord() { static record InnerRecord1(){} // violation below, 'Implied modifier 'static' should be explicit' record InnerRecord2(){} } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.modifier