Sub topic 8
Utility Classes and Private Constructors
A utility class is a class that groups related static methods. These methods perform an operation without needing data from a particular object. Examples include methods for string checks, mathematical calculations, or file-name handling.
This chapter assumes you can define classes, create objects with new, write methods, use private and public, and understand basic inheritance.
Why utility classes do not need objects
Suppose a class contains only a method that checks whether a string is blank. Creating a StringUtils object would not add useful information. The method can receive everything it needs as an argument.
public final class StringUtils {
private StringUtils() {
// Prevent callers from creating objects.
}
public static boolean isBlank(String text) {
return text == null || text.trim().isEmpty();
}
public static void main(String[] args) {
System.out.println(StringUtils.isBlank(" "));
System.out.println(StringUtils.isBlank("Java"));
}
}The method is static, so callers use the class name:
boolean result = StringUtils.isBlank(" ");They should not write this:
StringUtils utils = new StringUtils();
boolean result = utils.isBlank(" ");The utility class prevents that second form with a private constructor.
A constructor is a special method used when an object is created. A private constructor can be called only from inside its own class. Code outside the class therefore cannot use new StringUtils().
The class is also final. This prevents another class from extending it. Utility classes usually have no object state or object behavior to inherit, so extension would not be useful.
The utility class pattern
A typical utility class has these properties:
- The class is
final. - The constructor is
private. - The operations are
public staticmethods. - The class has no instance fields.
- The methods are usually stateless: their results depend on their arguments.
The private constructor is a design guard. It communicates that the class represents a collection of operations, not a kind of object.
What happens if the constructor is omitted?
If a class declares no constructor, Java supplies a public-like default constructor when the class itself is accessible. That allows callers to create instances accidentally.
public final class MathUtils {
public static int square(int number) {
return number * number;
}
}A caller may then write:
MathUtils value = new MathUtils();The instance is unnecessary. Adding a private constructor makes the intended use clear and causes an invalid construction attempt to fail at compile time.
When utility classes are a good fit
Use a utility class when an operation:
- Does not need object-specific state.
- Does not represent a meaningful object or service.
- Fits naturally with a small group of related operations.
- Can be called without configuring an instance.
Examples include converting units, validating simple text, or calculating a checksum.
Do not place unrelated methods in one large class. A class named AppUtils that contains database code, date formatting, validation, and network operations is difficult to understand and test. Prefer focused classes such as DateUtils and ValidationUtils.
A utility method can also make dependencies less visible. For example, a method that reads the system clock directly is harder to test than a method that receives a clock or time value. Use static utilities for simple, deterministic operations. Use ordinary objects when behavior needs configuration, replacement, or injected dependencies.
Exercise
Exercise
Why do utility classes commonly use both a private constructor and static methods?
Show answer
The private constructor prevents unnecessary objects from being created, while static methods can be called through the class name without an object.