Hide the constructor of the Utility class: Utility classes must not have an open or standard constructor

I get this warning from Sonar. I want the solution to remove this warning on the sonar. My class is as follows:

public class FilePathHelper { private static String resourcesPath; public static String getFilePath(HttpServletRequest request) { if(resourcesPath == null) { String serverpath=request.getSession().getServletContext().getRealPath(""); resourcesPath = serverpath + "/WEB-INF/classes/"; } return resourcesPath; } } 

I want the right solution to remove this warning on a sonar.

+78
java constructor sonarqube
Jan 18 '13 at 12:14
source share
9 answers

If this class is just a utility class, you must make the class final and define a private constructor:

 public final class FilePathHelper { private FilePathHelper() { //not called } } 

This prevents the use of a constructor without default parameters elsewhere in your code. In addition, you can make the class final so that it cannot be extended in subclasses, which is best practice for utility classes. Since you have declared only a private constructor, other classes will not be able to extend it, but it is still best to mark the class as final.

+150
Jan 18 '13 at 12:16
source share

I don't know Sonar, but I suspect he's looking for a private constructor:

 private FilePathHelper() { // No-op; won't be called } 

Otherwise, the Java compiler will provide an open, parameterless constructor that you really don't need.

(You should also make it final, although other classes cannot extend it, since it only has a private constructor.)

+18
Jan 18 '13 at 12:15
source share

I use enumeration without instances

 public enum MyUtils { ; // no instances // class is final and the constructor is private public static int myUtilityMethod(int x) { return x * x; } } 

you can call it using

 int y = MyUtils.myUtilityMethod(5); // returns 25. 
+11
Jan 18 '13 at 12:37
source share

Best practice is to throw an error if the class is built.

Example:

 /** * The Class FooUtilityService. */ final class FooUtilityService{ /** * Instantiates a new FooUtilityService. Private to prevent instantiation */ private FooUtilityService() { // Throw an exception if this ever *is* called throw new AssertionError("Instantiating utility class."); } 
+9
Jan 15 '15 at 18:35
source share
 public class LmsEmpWfhUtils { private LmsEmpWfhUtils() { // prevents access default paramater-less constructor } } 

This prevents the use of a default constructor without parameters elsewhere in your code.

0
May 24 '18 at 6:40
source share

Enumerations can be used as utility classes. This eliminates the errors of Sonar, Checkstyle and PMD. I limit my use of instances without instances to core classes. I do not want to use enum just to get around this rule.

Also, what do you do with the Java documentation that generates and displays all of these Utility classes as enumerations? This can be misleading and difficult to understand.

Can't the JDK provide an enumeration-like class for these specific utility classes? Essentially, we need a class that works just like enum, is final, and does not require a private constructor.

0
Jun 29 '19 at 2:00
source share

Add private constructor:

 private FilePathHelper(){ super(); } 
-one
May 04 '16 at 4:59
source share

make the utilities class final and add a private constructor

-one
Apr 19 '18 at 0:05
source share

SonarQube documentation recommends adding a static keyword to the class declaration.

That is, change the public class FilePathHelper to the public static class FilePathHelper .

Alternatively, you can add a private or secure constructor.

 public class FilePathHelper { // private or protected constructor // because all public fields and methods are static private FilePathHelper() { } } 
-one
Dec 17 '18 at 18:01
source share



All Articles