Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

SDCardUtils is a final utility class providing static methods for SD card detection and path retrieval (isSDCardEnableByEnvironment, getSDCardPath, getInternalStoragePath, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/SDCardUtils.java:26

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/11
 *     desc  : utils about sdcard
 * </pre>
 */
public final class SDCardUtils {

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

    /**
     * Return whether sdcard is enabled by environment.
     *
     * @return {@code true}: enabled<br>{@code false}: disabled
     */
    public static boolean isSDCardEnableByEnvironment() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /**
     * Return the path of sdcard by environment.
     *
     * @return the path of sdcard by environment
     */
    public static String getSDCardPathByEnvironment() {
        if (isSDCardEnableByEnvironment()) {

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: SDCardUtils.isSDCardEnableByEnvironment(), SDCardUtils.getSDCardPath(). Never call new SDCardUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline for testing.

Example fix

// before
SDCardUtils sdu = new SDCardUtils();
boolean enabled = sdu.isSDCardEnableByEnvironment();

// after
boolean enabled = SDCardUtils.isSDCardEnableByEnvironment();
Defensive patterns

Strategy: type-guard

Validate before calling

// SDCardUtils is static-only
boolean enabled = SDCardUtils.isSDCardEnableByEnvironment();
String sdPath = SDCardUtils.getSDCardPath();

Type guard

static boolean isStaticUtility(Class<?> c) {
    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith("Utils");
}

Try / catch

try {
    SDCardUtils.class.getDeclaredConstructor().setAccessible(true);
    SDCardUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
    // Use static methods
}

Prevention

When it happens

Trigger: Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection.

Common situations: DI component scanning traverses the utility package; a test framework instantiates all classes for coverage; a serialization library targets the class.

Related errors


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