Java utilities class setup issue [Abstract or Private Constructor]

19 Views Asked by At

When I am creating static utilities method, I always go with the below logic.

public class XXXUtils {
    // to prevent utilities class accidentally instantiated
    private XXXUtils() {}

    public static XXX getXXX(YYY yyy) {
        // ...
    }
}

which I think is the de-facto standard, and is used by jdk (e.g. java.util.Collections), guava (e.g. com.google.common.collect.Collections2).

However today when I am browsing the SpringMVC source code, I found that utilities class are created in the following.

public abstract class XXXUtils {
    public static XXX getXXX(YYY yyy) {
        // ...
    }
}

This is found in SpringWeb org.springframework.web.context.support.WebApplicationContextUtils. This is an interesting idea that I did not think of previously.

And of course, both mechanism works well for disallowing the instance from initializing. But which way is a better way of creating utilities class that only contains static field and methods. and Why?

0

There are 0 best solutions below