{"record":{"id":"c31a8e54e5bf6a60","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-c31a8e","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/FlashlightUtils.java","lineNumber":27,"sourceCode":"\nimport static android.hardware.Camera.Parameters.FLASH_MODE_OFF;\nimport static android.hardware.Camera.Parameters.FLASH_MODE_TORCH;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/04/27\n *     desc  : utils about flashlight\n * </pre>\n */\npublic final class FlashlightUtils {\n\n    private static Camera         mCamera;\n    private static SurfaceTexture mSurfaceTexture;\n\n    private FlashlightUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return whether the device supports flashlight.\n     *\n     * @return {@code true}: yes<br>{@code false}: no\n     */\n    public static boolean isFlashlightEnable() {\n        return Utils.getApp()\n                .getPackageManager()\n                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);\n    }\n\n    /**\n     * Return whether the flashlight is working.\n     *\n     * @return {@code true}: yes<br>{@code false}: no\n     */","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/FlashlightUtils.java#L9-L45","documentation":"FlashlightUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). It wraps the camera flash via static methods backed by static Camera/SurfaceTexture fields, so no instance is needed and the constructor only blocks instantiation, matching the library's *Utils convention.","triggerScenarios":"Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes by Class reference. A direct `new FlashlightUtils()` fails to compile because the constructor is private.","commonSituations":"Coverage tooling that instantiates all classes; DI containers scanning packages; a developer who assumes instance state per torch session; reflective test harnesses that exercise all constructors.","solutions":["Use the static API directly: FlashlightUtils.isFlashlightEnable(), FlashlightUtils.setFlashlightOn(...), etc.","Exclude final utility classes from any reflective instantiation framework.","In a contract test, assert UnsupportedOperationException is thrown by the reflected constructor."],"exampleFix":"// before (reflection)\nFlashlightUtils fu = FlashlightUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nif (FlashlightUtils.isFlashlightEnable()) { FlashlightUtils.setFlashlightOn(); }","handlingStrategy":"validation","validationCode":"// Never construct FlashlightUtils; it is static-only.\nif (clazz == FlashlightUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use FlashlightUtils.xxx() statically\n}","typeGuard":"if (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // static-only; do not newInstance()\n}","tryCatchPattern":"try {\n    Constructor<?> c = FlashlightUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use FlashlightUtils.xxx() instead\n}","preventionTips":["FlashlightUtils holds static Camera/SurfaceTexture state — there is no per-instance value.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call FlashlightUtils.isFlashlightEnable()/setFlashlightOn() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","flashlight"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}