Utility class in Java:
A Utility Class in Java is a class that contains only static methods (and sometimes static variables) which perform common, reusable operations. These are often helper classes that group together functions used throughout your codebase, such as string manipulation, file I/O helpers, date-time operations, etc.
Characteristics of a Utility Class:
1). All methods are static
2). It is not meant to be instantiated.
3). Mark the constructor private to prevent object creation.
Example :
public final class MathUtils {
// Private constructor to prevent instantiation
private MathUtils() {
throw new UnsupportedOperationException("Utility class");
}
public static int add(int a, int b) {
return a + b;
}
public static int max(int a, int b) {
return a > b ? a : b;
}
}