UncommentedMain

Since Checkstyle 3.2

Description

Detects uncommented 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:

public class Game {
   public static void main(String... args){}   // violation 'Uncommented main method found'
}

public class Main {
   public static void main(String[] args){}   // violation 'Uncommented main method found'
}

public class Launch {
   //public static void main(String[] args){} // OK
}

public class Start {
   public void main(){}                       // OK
}

public record MyRecord1 {
    public void main(){}                       // violation 'Uncommented main method found'
}

public record MyRecord2 {
    //public void main(){}                     // OK
}
        

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:

public class Game {
   public static void main(String... args){}   // violation 'Uncommented main method found'
}

public class Main {
   public static void main(String[] args){}   // OK
}

public class Launch {
   //public static void main(String[] args){} // OK
}

public class Start {
   public void main(){}                       // OK
}

public record MyRecord1 {
    public void main(){}                      // OK
}
        

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

Parent Module

TreeWalker