Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

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.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/FlashlightUtils.java:27

import static android.hardware.Camera.Parameters.FLASH_MODE_OFF;
import static android.hardware.Camera.Parameters.FLASH_MODE_TORCH;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/04/27
 *     desc  : utils about flashlight
 * </pre>
 */
public final class FlashlightUtils {

    private static Camera         mCamera;
    private static SurfaceTexture mSurfaceTexture;

    private FlashlightUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Return whether the device supports flashlight.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isFlashlightEnable() {
        return Utils.getApp()
                .getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    /**
     * Return whether the flashlight is working.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API directly: FlashlightUtils.isFlashlightEnable(), FlashlightUtils.setFlashlightOn(...), etc.
  2. Exclude final utility classes from any reflective instantiation framework.
  3. In a contract test, assert UnsupportedOperationException is thrown by the reflected constructor.

Example fix

// before (reflection)
FlashlightUtils fu = FlashlightUtils.class.getDeclaredConstructor().newInstance();

// after
if (FlashlightUtils.isFlashlightEnable()) { FlashlightUtils.setFlashlightOn(); }
Defensive patterns

Strategy: validation

Validate before calling

// Never construct FlashlightUtils; it is static-only.
if (clazz == FlashlightUtils.class || Modifier.isFinal(clazz.getModifiers())) {
    // skip instantiation; use FlashlightUtils.xxx() statically
}

Type guard

if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
    // static-only; do not newInstance()
}

Try / catch

try {
    Constructor<?> c = FlashlightUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use FlashlightUtils.xxx() instead
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/c31a8e54e5bf6a60. Report an issue: GitHub.