{"record":{"id":"ddc9c943802a07ff","repo":"JessYanCoding/AndroidAutoSize","slug":"you-can-t-instantiate-me-ddc9c9","errorCode":null,"errorMessage":"you can't instantiate me!","messagePattern":"you can't instantiate me!","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"autosize/src/main/java/me/jessyan/autosize/utils/ScreenUtils.java","lineNumber":37,"sourceCode":"import android.content.res.Resources;\nimport android.graphics.Point;\nimport android.os.Build;\nimport android.provider.Settings;\nimport android.util.DisplayMetrics;\nimport android.view.Display;\nimport android.view.WindowManager;\n\n/**\n * ================================================\n * Created by JessYan on 26/09/2016 16:59\n * <a href=\"mailto:jess.yan.effort@gmail.com\">Contact me</a>\n * <a href=\"https://github.com/JessYanCoding\">Follow me</a>\n * ================================================\n */\npublic class ScreenUtils {\n\n    private ScreenUtils() {\n        throw new IllegalStateException(\"you can't instantiate me!\");\n    }\n\n    public static int getStatusBarHeight() {\n        int result = 0;\n        try {\n            int resourceId = Resources.getSystem().getIdentifier(\"status_bar_height\", \"dimen\", \"android\");\n            if (resourceId > 0) {\n                result = Resources.getSystem().getDimensionPixelSize(resourceId);\n            }\n        } catch (Resources.NotFoundException e) {\n            e.printStackTrace();\n        }\n        return result;\n    }\n\n    /**\n     * 获取当前的屏幕尺寸\n     *","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/JessYanCoding/AndroidAutoSize/blob/e402ecdd998ea07549513ac08c12e9f097f3b48c/autosize/src/main/java/me/jessyan/autosize/utils/ScreenUtils.java#L19-L55","documentation":"ScreenUtils is a static utility class whose constructor is private and deliberately throws IllegalStateException(\"you can't instantiate me!\"). Instantiating it (new ScreenUtils()) is a misuse; all methods like getStatusBarHeight() are static. The exception exists purely to enforce the utility-class pattern.","triggerScenarios":"Writing new ScreenUtils() anywhere — directly in app code, via reflection (Class.newInstance / getDeclaredConstructor().newInstance()), or a dependency-injection framework attempting to construct it.","commonSituations":"Copy-paste habits from non-utility classes, DI frameworks scanning and trying to instantiate every class in a package, or reflection-based test scaffolding.","solutions":["Use the static methods directly: ScreenUtils.getStatusBarHeight(context) — remove the 'new'.","Exclude utility classes from DI/reflection classpath scanning.","In tests, do not instantiate; call the static methods instead."],"exampleFix":"// before\nScreenUtils utils = new ScreenUtils();\nint h = utils.getStatusBarHeight();\n\n// after\nint h = ScreenUtils.getStatusBarHeight();","handlingStrategy":"type-guard","validationCode":"public static boolean isUtilityClass(Class<?> clazz) {\n    try {\n        java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();\n        c.setAccessible(true);\n        return false; // instantiation attempt will throw for guarded utilities\n    } catch (Exception e) {\n        return true;\n    }\n}\n// Simply avoid 'new ScreenUtils()': check Modifier.isStatic usage patterns in review.","typeGuard":"public static boolean isInstantiable(Class<?> clazz) {\n    int mods = clazz.getModifiers();\n    return !java.lang.reflect.Modifier.isAbstract(mods)\n        && !clazz.getName().endsWith(\"Utils\")\n        && !clazz.getSimpleName().matches(\".*(Utils|Util|Preconditions|ScreenUtils)\");\n}","tryCatchPattern":"try {\n    ScreenUtils.class.getDeclaredConstructor().newInstance();\n} catch (InvocationTargetException e) {\n    // expected: constructor throws IllegalStateException\n    Log.w(\"Reflect\", \"ScreenUtils is a static utility class; use static methods\", e);\n}","preventionTips":["Call ScreenUtils.getStatusBarHeight() and other helpers statically; never construct the class.","Configure DI frameworks (Dagger/Spring-style scanners) to skip *Utils classes.","Keep the convention that classes named *Utils are static-only and un-instantiable.","When using reflection generically, filter out private constructors or catch IllegalStateException from the constructor."],"tags":["android","utility-class","instantiation","illegal-state"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"e402ecdd998ea07549513ac08c12e9f097f3b48c","analyzedAt":"2026-09-07T15:05:42.094Z","contentChangedAt":"2026-09-07T15:05:42.094Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}