Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

EncodeUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All encoding helpers (URL-encode, base64, html-encode, binary string) are static; the constructor exists only to prevent instantiation, consistent with the library-wide *Utils pattern.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/EncodeUtils.java:22

import android.text.Html;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/07
 *     desc  : utils about encode
 * </pre>
 */
public final class EncodeUtils {

    private EncodeUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Return the urlencoded string.
     *
     * @param input The input.
     * @return the urlencoded string
     */
    public static String urlEncode(final String input) {
        return urlEncode(input, "UTF-8");
    }

    /**
     * Return the urlencoded string.
     *
     * @param input       The input.
     * @param charsetName The name of charset.
     * @return the urlencoded string

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API: EncodeUtils.urlEncode(input), EncodeUtils.base64Encode(...), etc.
  2. Add EncodeUtils to the exclusion list of any tool that reflectively instantiates classes.
  3. For a contract test, expect UnsupportedOperationException from the reflected constructor.

Example fix

// before (reflection)
EncodeUtils eu = EncodeUtils.class.getDeclaredConstructor().newInstance();

// after
String enc = EncodeUtils.urlEncode("a b");
Defensive patterns

Strategy: validation

Validate before calling

// Never construct EncodeUtils; it is static-only.
if (clazz == EncodeUtils.class || Modifier.isFinal(clazz.getModifiers())) {
    // skip instantiation; use EncodeUtils.xxx() statically
}

Type guard

if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
    // static-only; do not newInstance()
}

Try / catch

try {
    Constructor<?> c = EncodeUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use EncodeUtils.xxx() instead
}

Prevention

When it happens

Trigger: Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes from Class references. A direct `new EncodeUtils()` fails to compile due to the private constructor.

Common situations: Coverage/quality tools that instantiate all classes; DI or reflective object mappers; a developer who expects instance state; service loaders that scan packages.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/451d28e76419783a. Report an issue: GitHub.