TheAlgorithms/Java · warning · UnsupportedOperationException

Utility class

Error message

Utility class

What it means

Thrown by the private constructor of UniqueSubsequencesCount when reflection or same-package code attempts to instantiate the utility class. The class is final with only static methods; instantiation is intentionally forbidden. Message: 'Utility class'.

Source

Thrown at src/main/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCount.java:26

 * Utility class to find the number of unique subsequences that can be
 * produced from a given string.
 *
 * <p> This class contains static methods to compute the unique subsequence count
 * using dynamic programming and recursion. It ensures that duplicate characters
 * are not counted multiple times in the subsequences.</p>
 *
 * <p> Author: https://github.com/Tuhinm2002 </p>
 */
public final class UniqueSubsequencesCount {

    /**
     * Private constructor to prevent instantiation of this utility class.
     * This class should only be used in a static context.
     *
     * @throws UnsupportedOperationException if attempted to instantiate.
     */
    private UniqueSubsequencesCount() {
        throw new UnsupportedOperationException("Utility class");
    }

    /**
     * Finds the number of unique subsequences that can be generated from
     * the given string.
     *
     * <p> This method initializes a dynamic programming (DP) array and invokes
     * the recursive helper function to compute the subsequence count.</p>
     *
     * @param str the input string from which subsequences are generated
     * @return the total count of unique subsequences
     */
    public static int countSubseq(String str) {

        // DP array initialized to store intermediate results
        int[] dp = new int[str.length() + 1];
        Arrays.fill(dp, -1);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Do not instantiate the class; call its static methods directly (e.g. UniqueSubsequencesCount.count(str)).
  2. Configure reflection/DI tooling to skip final utility classes or to use static-only access.
  3. Exclude the class from reflective instantiation lists.

Example fix

// before (reflection)
Object u = UniqueSubsequencesCount.class.getDeclaredConstructor().newInstance();

// after
int c = UniqueSubsequencesCount.count(str);
Defensive patterns

Strategy: type-guard

Validate before calling

// Do not instantiate; call the static method directly.
int count = UniqueSubsequencesCount.count(str);

Type guard

// Utility class - never instantiate. Access statically:
// UniqueSubsequencesCount.count(str)

Prevention

When it happens

Trigger: Calling new UniqueSubsequencesCount() via reflection (e.g. test frameworks, DI containers, reflective builders); attempting instantiation from within the same package.

Common situations: A reflection-based utility tries to instantiate every class; a DI framework scans the package and tries to construct it; a test inadvertently does new UniqueSubsequencesCount().

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/139724052c740abb. Report an issue: GitHub.