Hide Utility Class Constructor : Utility classes should not have a public or default constructor

JavaConstructorSonarqube

Java Problem Overview


I am getting this warning on Sonar. I want solution to remove this warning on Sonar Qube.

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.

Java Solutions


Solution 1 - Java

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

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

This prevents the default parameter-less constructor from being used elsewhere in your code.

Additionally, you can make the class final, so that it can't be extended in subclasses, which is a best practice for utility classes. Since you declared only a private constructor, other classes wouldn't be able to extend it anyway, but it is still a best practice to mark the class as final.

Solution 2 - Java

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

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

Otherwise the Java compiler will provide a public parameterless constructor, which you really don't want.

(You should also make the class final, although other classes wouldn't be able to extend it anyway due to it only having a private constructor.)

Solution 3 - Java

I use an enum with no 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 this using

int y = MyUtils.myUtilityMethod(5); // returns 25.

Solution 4 - Java

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

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.");
}

Solution 5 - Java

You can just use Lombok annotation to avoid unnecessary initialization.

Using @NoArgsConstructor with AccessLevel.PRIVATE as bellow:

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FilePathHelper {
   // your code 
}

Solution 6 - Java

I recommend just disabling this rule in Sonar, there is no real benefit of introducing a private constructor, just redundant characters in your codebase other people need to read and computer needs to store and process.

Solution 7 - Java

Alternative using Lombok is use @UtilityClass annotation.

@UtilityClass was introduced as an experimental feature in Lombok v1.16.2:

> If a class is annotated with @UtilityClass, > the following things happen to it: > - It is marked final. > - If any constructors are declared in it, an error is generated. > - Otherwise, a private no-args constructor is generated; it throws a UnsupportedOperationException. > - All methods, inner classes, and fields in the class are marked static.

Overview:

A utility class is a class that is just a namespace for functions. No instances of it can exist, and all its members are static. For example, java.lang.Math and java.util.Collections are well known utility classes.

This annotation automatically turns the annotated class into one.

A utility class cannot be instantiated.

By marking your class with @UtilityClass, lombok will automatically generate a private constructor that throws an exception, flags as error any explicit constructors you add, and marks the class final.

If the class is an inner class, the class is also marked static.

All members of a utility class are automatically marked as static. Even fields and inner classes.

Example:
import lombok.experimental.UtilityClass;

@UtilityClass
public class FilePathHelper {

    private static String resourcesPath;

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

Reference from official documentation:

Solution 8 - Java

Add private constructor:

private FilePathHelper(){
    super();
}

Solution 9 - Java

public class LmsEmpWfhUtils {    
	private LmsEmpWfhUtils() 
    { 
    // prevents access default paramater-less constructor
	}
}

This prevents the default parameter-less constructor from being used elsewhere in your code.

Solution 10 - Java

SonarQube documentation recommends adding static keyword to the class declaration.

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

Alternatively you can add a private or protected constructor.

public class FilePathHelper
{
    // private or protected constructor
    // because all public fields and methods are static
    private FilePathHelper() {
    }
}

Solution 11 - Java

make the utility class final and add a private constructor

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOomph FortuityView Question on Stackoverflow
Solution 1 - JavaRoflcoptrExceptionView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaPeter LawreyView Answer on Stackoverflow
Solution 4 - JavajavaPlease42View Answer on Stackoverflow
Solution 5 - JavaRoman TopalovView Answer on Stackoverflow
Solution 6 - Javauser431640View Answer on Stackoverflow
Solution 7 - JavaℛɑƒæĿᴿᴹᴿView Answer on Stackoverflow
Solution 8 - Javachethan s jView Answer on Stackoverflow
Solution 9 - JavaRinku GohelView Answer on Stackoverflow
Solution 10 - JavapamcevoyView Answer on Stackoverflow
Solution 11 - JavaAmit AgrawalView Answer on Stackoverflow