{"record":{"id":"a3694d4bf118d6f7","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-a3694d","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/ScreenUtils.java","lineNumber":35,"sourceCode":"import android.view.Surface;\nimport android.view.View;\nimport android.view.Window;\nimport android.view.WindowManager;\n\nimport static android.Manifest.permission.WRITE_SETTINGS;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about screen\n * </pre>\n */\npublic final class ScreenUtils {\n\n    private ScreenUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return the width of screen, in pixel.\n     *\n     * @return the width of screen, in pixel\n     */\n    public static int getScreenWidth() {\n        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);\n        if (wm == null) return -1;\n        Point point = new Point();\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {\n            wm.getDefaultDisplay().getRealSize(point);\n        } else {\n            wm.getDefaultDisplay().getSize(point);\n        }\n        return point.x;\n    }","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java#L17-L53","documentation":"ScreenUtils is a final utility class whose only constructor is private and throws UnsupportedOperationException. The class exposes only static methods for screen dimensions/orientation, so it must never be instantiated. The throw is a guard: it fires the moment anything bypasses compile-time access control and invokes the constructor via reflection.","triggerScenarios":"Triggered when code calls ScreenUtils.class.getDeclaredConstructor().setAccessible(true).newInstance() (directly or transitively). Common transitive causes: Gson/Jackson deserializing into a ScreenUtils field, Mockito/PowerMock attempting to instantiate the class for mocking, Kotlin reflection, or a DI container scanning the classpath.","commonSituations":"Unit tests where a test runner or mock framework tries to construct utility classes; JSON deserialization that maps a field to ScreenUtils; accidental `new ScreenUtils()` after removing the final keyword in a fork; code-coverage tools (JaCoCo) generating reflective probes.","solutions":["Do not instantiate ScreenUtils — call its static methods directly, e.g. ScreenUtils.getScreenWidth().","If a serialization framework tries to build it, add a TypeAdapter/InstanceCreator (Gson) or a mixin (Jackson) that returns null or a sentinel rather than invoking the constructor.","If a mock framework is the culprit, configure it with Mockito.mockStatic (Mockito 3.4+) or use PowerMock on the static method rather than constructing the class.","If you genuinely need an instance, subclass or wrap the static API behind your own injectable component instead of breaking the constructor."],"exampleFix":"// before (Gson tries to construct the field type)\npublic class Config { ScreenUtils screen; }\nnew Gson().fromJson(json, Config.class);\n\n// after\npublic class Config { int screenWidth; }\nint w = ScreenUtils.getScreenWidth();","handlingStrategy":"validation","validationCode":"// Before any reflective construction, check it is not a static-only utility.\nif (Modifier.isFinal(ScreenUtils.class.getModifiers())\n    && ScreenUtils.class.getDeclaredConstructors()[0].getParameterCount() == 0) {\n  throw new IllegalStateException(\"ScreenUtils is a static utility; do not instantiate.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never write code that instantiates utility classes; treat final + private-constructor as a hard contract.","Configure Gson/Jackson with InstanceCreator/mixins for any utility-typed fields.","Use Mockito.mockStatic for static APIs instead of constructing the class.","Add a static-analysis check (ErrorProne/ForbiddenApi) banning getDeclaredConstructor on util classes."],"tags":["android","utility-class","reflection","instantiation"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}