TheAlgorithms/Java · error · UnsupportedOperationException
This is a utility class and cannot be instantiated.
Error message
This is a utility class and cannot be instantiated.
What it means
Thrown by the private constructor of the Average utility class when an attempt is made to instantiate it. Average is a final class containing only static methods; the constructor deliberately throws UnsupportedOperationException to enforce the utility-class pattern. The JVM prevents external instantiation via the private modifier, but reflection or an internal subclass (not possible since the class is final) could theoretically trigger this.
Solutions
- Do not instantiate Average — call its static methods directly (Average.average(arr), Average.averageStream(arr)).
- If a reflection framework is auto-instantiating, exclude utility classes or annotate them to be skipped.
- If scanning code is the culprit, filter out classes with only static methods or private constructors before attempting instantiation.
Example fix
// before (reflection triggers the exception)
Average avg = Average.class.getDeclaredConstructor().setAccessible(true).newInstance();
// after (use static methods — never instantiate)
double mean = Average.average(new double[]{1.0, 2.0, 3.0}); Defensive patterns
Strategy: type-guard
Validate before calling
// Never instantiate Average — use static methods directly
double mean = Average.average(new double[]{1.0, 2.0, 3.0});
// If using a reflection framework, skip classes with private constructors:
if (clazz == Average.class) continue; // skip utility class Type guard
// Check whether a class should be instantiated (utility-class guard)
static boolean isInstantiable(Class<?> clazz) {
try {
Constructor<?> c = clazz.getDeclaredConstructor();
return java.lang.reflect.Modifier.isPublic(c.getModifiers());
} catch (NoSuchMethodException e) {
return false;
}
} Try / catch
// If reflectively scanning packages, catch and skip utility classes
try {
Object instance = clazz.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
// Skip utility classes that refuse instantiation
continue;
} Prevention
- Always call Average methods statically — never use reflection to instantiate utility classes.
- If a DI or scanning framework auto-instantiates classes, exclude or annotate utility classes.
- Add a filter for classes with private constructors or no public constructor before reflection-based instantiation.
When it happens
Trigger: Attempting to instantiate Average via reflection: Average.class.getDeclaredConstructor().setAccessible(true).newInstance(). Calling new Average() directly fails at compile time due to the private constructor, so this only manifests at runtime through reflection.
Common situations: A reflection-based framework (dependency injection, serialization library, ORM) that auto-discovers and instantiates classes by scanning packages or annotations. A test utility that tries to instantiate all classes in a package for coverage analysis. A generic factory that reflectively creates instances of any class it encounters.
Related errors
- No instances.
- Utility class
- A padding '=' must not be followed by a non-padding…
- Base must be greater than 01
- Base must be greater than 02
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/291a31cb67353595.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/com/thealgorithms/maths/Average.java:19
package com.thealgorithms.maths;
import java.util.Arrays;
import java.util.OptionalDouble;
/**
* A utility class for computing the average of numeric arrays.
*
* <p>This class provides static methods to calculate the arithmetic mean
* of arrays of both {@code double} and {@code int} values. It also offers
* a Stream-based alternative for modern, declarative usage.
*
* <p>All methods guard against {@code null} or empty inputs.
*/
public final class Average {
// Prevent instantiation of this utility class
private Average() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated.");
}
/**
* Computes the arithmetic mean of a {@code double} array.
*
* <p>The average is calculated as the sum of all elements divided
* by the number of elements: {@code avg = Σ(numbers[i]) / n}.
*
* @param numbers a non-null, non-empty array of {@code double} values
* @return the arithmetic mean of the given numbers
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
*/
public static double average(double[] numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
double sum = 0;
for (double number : numbers) {View on GitHub (pinned to fdfb9a395b)