OuterTypeNumber

Since Checkstyle 5.0

Description

Checks for the number of types declared at the outer (or root) level in a file.

Rationale: It is considered good practice to only define one outer type per file.

Properties

name description type default value since
max Specify the maximum number of outer types allowed. int 1 5.0

Examples

To configure the check to accept 1 outer type per file:

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

Example:

// violation 2 lines above 'Outer types defined is 3 (max allowed is 1)'

public class Example1 {
}

class Example {
  void exampleMethod() {}
}

enum outer {
}
        

To configure the check to accept 2 outer types per file:

<module name="Checker">
  <module name="TreeWalker">
    <module name="OuterTypeNumber">
      <property name="max" value="3"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {
}

class Examples {
  void exampleMethod() {}
}

enum outer1 {
}
        

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.sizes

Parent Module

TreeWalker