Suppose I've a utility class which contains only static methods and variables. e.g:
public abstract final class StringUtils
{
public static final String NEW_LINE = System.getProperty("line.separator");
public static boolean isNotNullOrSpace(final String string)
{
return !(string == null || string.length() < 1 || string.trim().length() < 1);
}
}
In this scenario, it makes sense to make the class both abstract and final. Abstract because making an object of this class will be of no use as all methods are accessible statically. Final because the derived class cannot inherit anything from this class as it does not have any non-static member.
C# allows static modifier for such classes. Why doesn't Java support this?
A final
class can't be extended, an abstract
class needs to be extended in order to be instantiated. Therefore, a final abstract
class would be a logical contradiction.
If your class just have static
methods, maybe you should just hide
its constructor, by defining it as private
.-
private StringUtils() {
}