OneTopLevelClass

Since Checkstyle 5.8

Description

Checks that each top-level class, interface, enum or annotation resides in a source file of its own. Official description of a 'top-level' term: 7.6. Top Level Type Declarations. If file doesn't contain public class, interface, enum or annotation, top-level type is the first type in file.

Examples

To configure the check:

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

ATTENTION: This Check does not support customization of validated tokens, so do not use the "tokens" property.

An example of code with violations:

public class Example1 { // OK, first top-level class
  // methods
}

class ViolationExample1 { // violation, "has to reside in its own source file."
  // methods
}
        

An example of code without public top-level type:

class Example2 { // OK, first top-level class
  // methods
}

class ViolationExample2 { // violation, "has to reside in its own source file."
  // methods
}
        

An example of code without violations:

public class Example3 { // OK, only one top-level class
  // methods
}
        

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

Parent Module

TreeWalker