I am getting this warning on Sonar.I want solution to remove this warning on sonar. My class is like this :
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 proper solution to remove this warning on sonar.
I don't know Sonar, but I suspect it's looking for a private constructor:
Otherwise the Java compiler will provide a public parameterless constructor, which you really don't want.
(You should also make it final, although other classes wouldn't be able to extend it anyway due to it only having a private constructor.)
I use an enum with no instances
you can call this using
Best practice is to throw an error if the class is constructed.
Example:
Add private constructor:
SonarQube documentation recommends adding
static
keyword to the class declaration.That is, change
public class FilePathHelper
topublic static class FilePathHelper
.Alternatively you can add a private or protected constructor.
This prevents the default parameter-less constructor from being used elsewhere in your code.