{"record":{"id":"6e38aa2116626e68","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-6e38aa","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/ColorUtils.java","lineNumber":22,"sourceCode":"import androidx.annotation.ColorInt;\nimport androidx.annotation.ColorRes;\nimport androidx.annotation.FloatRange;\nimport androidx.annotation.IntRange;\nimport androidx.annotation.NonNull;\nimport androidx.core.content.ContextCompat;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2019/01/15\n *     desc  : utils about color\n * </pre>\n */\npublic final class ColorUtils {\n\n    private ColorUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Returns a color associated with a particular resource ID.\n     *\n     * @param id The desired resource identifier.\n     * @return a color associated with a particular resource ID\n     */\n    public static int getColor(@ColorRes int id) {\n        return ContextCompat.getColor(Utils.getApp(), id);\n    }\n\n    /**\n     * Set the alpha component of {@code color} to be {@code alpha}.\n     *\n     * @param color The color.\n     * @param alpha Alpha component \\([0..255]\\) of the color.\n     * @return the {@code color} with {@code alpha} component","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ColorUtils.java#L4-L40","documentation":"ColorUtils is a final utility class whose only constructor is private and throws UnsupportedOperationException(\"u can't instantiate me...\"). All functionality is exposed as static methods, so no instance is ever needed. The constructor exists purely to block subclassing/instantiation; the message is the library's standard guard across all *Utils classes.","triggerScenarios":"Reflection-based instantiation: getDeclaredConstructor().setAccessible(true).newInstance(), or passing the Class to a reflection utility. Direct `new ColorUtils()` will not compile because the constructor is private, so this only surfaces at runtime via reflection.","commonSituations":"Code-coverage/quality tools that walk all classes and instantiate them; test harnesses verifying the no-instantiation contract; a developer wrongly assuming instance methods are required; dependency-injection containers that scan and instantiate classes reflectively.","solutions":["Stop trying to construct an instance: every ColorUtils method is static, call them as ColorUtils.getColor(...) etc.","If a tool/framework forces instantiation, configure it to skip final utility classes or add an exclusion for ColorUtils.","For a test asserting the contract, expect UnsupportedOperationException (or AssertionError if you use AssertJ's assertThatThrownBy) rather than trying to use the instance."],"exampleFix":"// before (fails via reflection)\nColorUtils cu = ColorUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nint color = ColorUtils.getColor(R.color.my_color);","handlingStrategy":"validation","validationCode":"// Never construct ColorUtils; it is static-only.\n// If a framework forces instantiation, skip final utility classes:\nif (clazz == ColorUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; call static methods instead\n}","typeGuard":"// Reflection should not target final *Utils classes\nif (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // do not attempt newInstance(); use static API\n}","tryCatchPattern":"try {\n    Constructor<?> c = ColorUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected: class is static-only. Use ColorUtils.xxx() instead.\n}","preventionTips":["All *Utils classes in this library are static-only — never write or generate `new XxxUtils()`.","Configure coverage/DI/mutation tools to skip classes ending in 'Utils' or marked final.","Prefer compile-time static calls; any reflective newInstance() on a util class is a smell."],"tags":["utility-class","instantiation","design-pattern","reflection"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}