{"record":{"id":"a35a7125cebb544c","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-a35a71","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"info","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/BarUtils.java","lineNumber":51,"sourceCode":" * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/09/23\n *     desc  : utils about bar\n * </pre>\n */\npublic final class BarUtils {\n\n    ///////////////////////////////////////////////////////////////////////////\n    // status bar\n    ///////////////////////////////////////////////////////////////////////////\n\n    private static final String TAG_STATUS_BAR = \"TAG_STATUS_BAR\";\n    private static final String TAG_OFFSET = \"TAG_OFFSET\";\n    private static final int KEY_OFFSET = -123;\n\n    private BarUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return the status bar's height.\n     *\n     * @return the status bar's height\n     */\n    public static int getStatusBarHeight() {\n        Resources resources = Resources.getSystem();\n        int resourceId = resources.getIdentifier(\"status_bar_height\", \"dimen\", \"android\");\n        return resources.getDimensionPixelSize(resourceId);\n    }\n\n    /**\n     * Set the status bar's visibility.\n     *\n     * @param activity  The activity.\n     * @param isVisible True to set status bar visible, false otherwise.","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/BarUtils.java#L33-L69","documentation":"BarUtils is a final utility class exposing only static methods; its private constructor throws UnsupportedOperationException('u can't instantiate me...') to forbid instantiation. The throw is an intentional design guard, not a runtime fault. It is reachable only via reflection that bypasses the private modifier, since the class is final and the constructor is private.","triggerScenarios":"Using reflection (Constructor.setAccessible(true) then newInstance()) to invoke the private constructor; test/coverage tooling that instantiates private constructors for coverage; bytecode manipulators or frameworks that auto-instantiate classes.","commonSituations":"JaCoCo/Cobertura coverage runs that try to cover private constructors; reflection-based utility tests that blanket-instantiate all classes; DI or serialization frameworks that attempt no-arg construction.","solutions":["Do not instantiate BarUtils — call its static methods directly (e.g., BarUtils.getStatusBarHeight()).","If a coverage tool flags the constructor, exclude it or use the generated coverage helper rather than reflection.","In tests, assert the constructor is private via a reflection test that expects UnsupportedOperationException rather than trying to succeed.","Remove any reflective newInstance() call against BarUtils.class."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Never instantiate; call static methods directly.\nint h = BarUtils.getStatusBarHeight();","typeGuard":null,"tryCatchPattern":"try {\n    Constructor<BarUtils> c = BarUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException | InvocationTargetException e) {\n    // expected: instantiation is forbidden by design\n}","preventionTips":["Call BarUtils static methods directly; never reflectively instantiate it.","Exclude private constructors from coverage tooling that auto-instantiates.","For guard tests, assert UnsupportedOperationException instead of trying to succeed."],"tags":["utility-class","private-constructor","reflection","barutils","android","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}