The error, utility classes should not have a public or default constructor, you notice mainly in utility classes where you have declared your  class method(s) as public static and your class does not have private constructor as well as your class has not been declared as final.

This error occurs due to the checkstyle rules defined generally in checkstyle.xml that is used to check the quality of the written Java code.

This not only occurs due to the use of checkstyle plugin but also due to the use of SonarQube or other static code quality checking tool for Java code.

Recommended reading: How to use checkstyle plugin in gradle based project

Problem Scenario

I was also working on gradle based Spring Boot application and when I executed the checkstyle plugin then I saw this error on the utility classes where I did not declare the class as final but I had private constructor in each class. So it was very easy to fix the problem in those utility classes. So I just made sure that those utility classes must have private constructor and the classes should be declared as final to remove such issue.

But when executed the checkstyle rules on Spring Boot main class I also found the same issue. Now neither I can declare the @SpringBootApplication main class final nor I can have a private constructor in Spring Boot main class because it is not a utility class. If you make this class final and if you declare any private constructor in this main class Spring will complain and won’t be able to run your application. Therefore I cannot use the same solution for this Spring Boot main class. So I suppressed this issue for this main class including the rule in the supressions.xml file that will be included into checkstyle.xml file in order to make it work.

The error you find in the Spring Boot main class generally looks similar to the below image:

Utility classes should not have a public or default constructor – Spring Boot


Java 1.8, Eclipse, Spring Boot 1.5.9, Gradle 4.x

How to use checkstyle on gradle based Java project before reading forward.

Example with Source Code

Now follow below steps in order to make it happen:

Creating XML

Create supressions.xml file in the same directory (<projet’s root directory>/config/checkstyle).

Put below content into supressions.xml file.

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.0//EN"
    <suppress files="" checks="HideUtilityClassConstructor" />

Including XML

Include the supressions.xml file into checkstyle.xml file by using the below code. The variable ${config_loc}‘s value is resolved when you use the default location for checkstyle plugin. If you use location other than default then make sure the file is included.

<module name="SuppressionFilter">
	<property name="file" value="${config_loc}/suppressions.xml" />

Executing Chekstyle

Now execute the checkstyle rules, you should see issue, utility classes should not have a public or default constructor, has gone away from main class as well.

That’s all. Hope you got idea how to remove checkstyle issues from utility classes as well from @SpringBootApplication main class.

Recommended reading: How to use checkstyle plugin in gradle based project

Thanks for reading.


Leave a Reply

Your email address will not be published. Required fields are marked *