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 Foo { // OK, first top-level class
  // methods
}

class Foo2 { // violation, second top-level class
  // methods
}

record Foo3 { // violation, third top-level "class"
  // methods
}
        

An example of code without public top-level type:

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

class Foo2 { // violation, second top-level class
  // methods
}
        

An example of code without violations:

public class Foo { // 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