{"record":{"id":"1472e99f09ef5e8e","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-1472e9","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/Utils.java","lineNumber":36,"sourceCode":" *      /  /:/~/::\\  /__/\\   /  /\\  /  /:/~/::\\   _____\\__\\:\\   __|  |:|     \\__\\/\\:\\\n *     /__/:/ /:/\\:| \\  \\:\\ /  /:/ /__/:/ /:/\\:\\ /__/::::::::\\ /__/\\_|:|____    \\  \\:\\\n *     \\  \\:\\/:/~/:/  \\  \\:\\  /:/  \\  \\:\\/:/__\\/ \\  \\:\\~~\\~~\\/ \\  \\:\\/:::::/     \\__\\:\\\n *      \\  \\::/ /:/    \\  \\:\\/:/    \\  \\::/       \\  \\:\\  ~~~   \\  \\::/~~~~      /  /:/\n *       \\  \\:\\/:/      \\  \\::/      \\  \\:\\        \\  \\:\\        \\  \\:\\         /__/:/\n *        \\  \\::/        \\__\\/        \\  \\:\\        \\  \\:\\        \\  \\:\\        \\__\\/\n *         \\__\\/                       \\__\\/         \\__\\/         \\__\\/\n *     blog  : http://blankj.com\n *     time  : 16/12/08\n *     desc  : utils about initialization\n * </pre>\n */\npublic final class Utils {\n\n    @SuppressLint(\"StaticFieldLeak\")\n    private static Application sApp;\n\n    private Utils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Init utils.\n     * <p>Init it in the class of UtilsFileProvider.</p>\n     *\n     * @param app application\n     */\n    public static void init(final Application app) {\n        if (app == null) {\n            Log.e(\"Utils\", \"app is null.\");\n            return;\n        }\n        if (sApp == null) {\n            sApp = app;\n            UtilsBridge.init(sApp);\n            UtilsBridge.preLoad();\n            return;","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java#L18-L54","documentation":"Utils is the core initialization class for the whole AndroidUtilCode library. Its private constructor throws UnsupportedOperationException to prevent instantiation. The class holds a static Application reference (sApp) and exposes static init()/getApp(); it is the entry point all other utils depend on, so it must never be constructed.","triggerScenarios":"Reflective instantiation via Utils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan.","commonSituations":"Serialization frameworks binding a field to Utils; mock frameworks in tests; coverage probes; accidental `new Utils()` after weakening modifiers. Because Utils is the library root, constructing it also implies the app never called Utils.init(app).","solutions":["Initialize once in Application.onCreate via Utils.init(this) (or via UtilsFileProvider), then use Utils.getApp() instead of constructing the class.","Provide a Gson InstanceCreator / Jackson mixin returning a sentinel so the constructor is not invoked.","Use Mockito.mockStatic for mocking rather than constructing Utils.","Keep the class final with a private constructor."],"exampleFix":"// before\nUtils u = new Utils();\n\n// after\n// in Application.onCreate()\nUtils.init(this);\nContext ctx = Utils.getApp();","handlingStrategy":"validation","validationCode":"if (Modifier.isFinal(Utils.class.getModifiers())) {\n  throw new IllegalStateException(\"Utils is static-only; call Utils.init() then Utils.getApp().\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call Utils.init(application) once at startup; use Utils.getApp() thereafter.","Register UtilsFileProvider in the manifest for automatic init.","Register InstanceCreator/mixin for JSON fields typed as Utils.","Use Mockito.mockStatic for mocking rather than constructing."],"tags":["android","utility-class","reflection","instantiation","init"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}