Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ConvertUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). Every conversion routine (hex, base64, byte/string, memory size, etc.) is static; the constructor exists only to enforce the static-only usage contract shared by all *Utils classes in the library.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ConvertUtils.java:48

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/13
 *     desc  : utils about convert
 * </pre>
 */
public final class ConvertUtils {

    private static final int    BUFFER_SIZE      = 8192;
    private static final char[] HEX_DIGITS_UPPER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static final char[] HEX_DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

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

    /**
     * Int to hex string.
     *
     * @param num The int number.
     * @return the hex string
     */
    public static String int2HexString(int num) {
        return Integer.toHexString(num);
    }

    /**
     * Hex string to int.
     *
     * @param hexString The hex string.
     * @return the int
     */

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API directly: ConvertUtils.bytes2HexString(...), ConvertUtils.byte2FitMemorySize(...), etc.
  2. Exclude final utility classes from any tooling that reflectively instantiates classes.
  3. If writing a no-instance-contract unit test, assert UnsupportedOperationException is thrown rather than consuming the instance.

Example fix

// before (reflection)
ConvertUtils cu = ConvertUtils.class.getDeclaredConstructor().newInstance();

// after
String hex = ConvertUtils.bytes2HexString(new byte[]{1, 2});
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Reflection-based construction via getDeclaredConstructor().newInstance() (with setAccessible), or handing the Class object to a reflective factory. A plain `new ConvertUtils()` is a compile error, so this is purely a runtime reflection artifact.

Common situations: Coverage/mutation-testing tools that instantiate every loaded class; DI frameworks that auto-discover and new-up classes; a developer who assumes the conversion helpers are instance methods; generic test utilities that reflect over a package.

Related errors


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