{"record":{"id":"a75d38df77e1212d","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-a75d38","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/LanguageUtils.java","lineNumber":30,"sourceCode":"\nimport androidx.annotation.NonNull;\nimport androidx.annotation.Nullable;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2019/06/20\n *     desc  : utils about language\n * </pre>\n */\npublic class LanguageUtils {\n\n    private static final String KEY_LOCALE          = \"KEY_LOCALE\";\n    private static final String VALUE_FOLLOW_SYSTEM = \"VALUE_FOLLOW_SYSTEM\";\n\n    private LanguageUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Apply the system language.\n     */\n    public static void applySystemLanguage() {\n        applySystemLanguage(false);\n    }\n\n    /**\n     * Apply the system language.\n     *\n     * @param isRelaunchApp True to relaunch app, false to recreate all activities.\n     */\n    public static void applySystemLanguage(final boolean isRelaunchApp) {\n        applyLanguageReal(null, isRelaunchApp);\n    }\n","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/LanguageUtils.java#L12-L48","documentation":"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.","triggerScenarios":"Direct or reflective instantiation via DI frameworks, mocking libraries, serialization frameworks (Gson/Jackson), or manual reflection with Constructor.newInstance().","commonSituations":"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.","solutions":["Use only static methods: LanguageUtils.applySystemLanguage(), LanguageUtils.getCurrentLocale(). Never call new LanguageUtils().","Exclude utility classes from DI scanning and serialization configs.","Mock static methods with mockito-inline/PowerMock rather than instantiating."],"exampleFix":"// before\nLanguageUtils lang = new LanguageUtils();\nlang.applySystemLanguage();\n\n// after\nLanguageUtils.applySystemLanguage();","handlingStrategy":"type-guard","validationCode":"// Verify before reflective instantiation\nif (!LanguageUtils.class.getName().endsWith(\"Utils\")) {\n    LanguageUtils.class.getDeclaredConstructor().newInstance();\n}","typeGuard":"static boolean isStaticUtility(Class<?> clazz) {\n    try {\n        java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();\n        c.setAccessible(true);\n        // Check if constructor body throws — can't know without calling,\n        // so use naming convention\n        return clazz.getSimpleName().endsWith(\"Utils\");\n    } catch (NoSuchMethodException e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    LanguageUtils.class.getDeclaredConstructor().setAccessible(true);\n    LanguageUtils instance = LanguageUtils.class.newInstance();\n} catch (UnsupportedOperationException e) {\n    // Switch to static access\n}","preventionTips":["Never instantiate classes ending in 'Utils'.","Exclude util packages from DI and serialization scanning.","Use static methods exclusively for utility classes."],"tags":["java","android","utility-class","instantiation","locale"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}