UncommentedMain
Since Checkstyle 3.2
Description
main
methods.
Rationale: A main
method is often used for debugging
purposes. When debugging is finished, developers often forget
to remove the method, which changes the API and increases the
size of the resulting class or JAR file. Except for
the real program entry points, all main
methods should be
removed or commented out of the sources.
Properties
name | description | type | default value | since |
---|---|---|---|---|
excludedClasses | Specify pattern for qualified names of classes which are allowed to have a main method. |
Pattern | "^$" |
3.2 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="UncommentedMain"/>
</module>
</module>
Example:
class Example1 {
// violation below, 'Uncommented main method found'
public static void main(String... args){}
}
class Main {
// violation below, 'Uncommented main method found'
public static void main(String[] args){}
}
class Launch {
//public static void main(String[] args){}
}
class Start {
public void main(){}
}
record MyRecord1() {
public void main(){}
}
record MyRecord2() {
//public void main(){}
}
To configure the check to allow the main
method for all classes
with "Main" name:
<module name="Checker">
<module name="TreeWalker">
<module name="UncommentedMain">
<property name="excludedClasses" value="\.Main$"/>
</module>
</module>
</module>
Example:
class Example2 {
// violation below, 'Uncommented main method found'
public static void main(String... args){}
}
class Main {
public static void main(String[] args){}
}
class Launch {
//public static void main(String[] args){}
}
class Start {
public void main(){}
}
record MyRecord1() {
public void main(){}
}
record MyRecord2() {
//public void main(){}
}
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