Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

LanguageUtils provides static methods for applying and retrieving locale/language settings in Android apps. Its private constructor throws UnsupportedOperationException to enforce static-only usage. Note: unlike most utility classes in this library, LanguageUtils is declared as a non-final public class, but the constructor guard still applies.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/LanguageUtils.java:30

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2019/06/20
 *     desc  : utils about language
 * </pre>
 */
public class LanguageUtils {

    private static final String KEY_LOCALE          = "KEY_LOCALE";
    private static final String VALUE_FOLLOW_SYSTEM = "VALUE_FOLLOW_SYSTEM";

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

    /**
     * Apply the system language.
     */
    public static void applySystemLanguage() {
        applySystemLanguage(false);
    }

    /**
     * Apply the system language.
     *
     * @param isRelaunchApp True to relaunch app, false to recreate all activities.
     */
    public static void applySystemLanguage(final boolean isRelaunchApp) {
        applyLanguageReal(null, isRelaunchApp);
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use only static methods: LanguageUtils.applySystemLanguage(), LanguageUtils.getCurrentLocale(). Never call new LanguageUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline/PowerMock rather than instantiating.

Example fix

// before
LanguageUtils lang = new LanguageUtils();
lang.applySystemLanguage();

// after
LanguageUtils.applySystemLanguage();
Defensive patterns

Strategy: type-guard

Validate before calling

// Verify before reflective instantiation
if (!LanguageUtils.class.getName().endsWith("Utils")) {
    LanguageUtils.class.getDeclaredConstructor().newInstance();
}

Type guard

static boolean isStaticUtility(Class<?> clazz) {
    try {
        java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();
        c.setAccessible(true);
        // Check if constructor body throws — can't know without calling,
        // so use naming convention
        return clazz.getSimpleName().endsWith("Utils");
    } catch (NoSuchMethodException e) {
        return false;
    }
}

Try / catch

try {
    LanguageUtils.class.getDeclaredConstructor().setAccessible(true);
    LanguageUtils instance = LanguageUtils.class.newInstance();
} catch (UnsupportedOperationException e) {
    // Switch to static access
}

Prevention

When it happens

Trigger: Direct or reflective instantiation via DI frameworks, mocking libraries, serialization frameworks (Gson/Jackson), or manual reflection with Constructor.newInstance().

Common situations: A DI framework or component scanner targets the util package; a mocking framework tries to instantiate for partial mocking; a serialization library attempts to create an instance.

Related errors


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