Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

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.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/BarUtils.java:51

 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/23
 *     desc  : utils about bar
 * </pre>
 */
public final class BarUtils {

    ///////////////////////////////////////////////////////////////////////////
    // status bar
    ///////////////////////////////////////////////////////////////////////////

    private static final String TAG_STATUS_BAR = "TAG_STATUS_BAR";
    private static final String TAG_OFFSET = "TAG_OFFSET";
    private static final int KEY_OFFSET = -123;

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

    /**
     * Return the status bar's height.
     *
     * @return the status bar's height
     */
    public static int getStatusBarHeight() {
        Resources resources = Resources.getSystem();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    /**
     * Set the status bar's visibility.
     *
     * @param activity  The activity.
     * @param isVisible True to set status bar visible, false otherwise.

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Do not instantiate BarUtils — call its static methods directly (e.g., BarUtils.getStatusBarHeight()).
  2. If a coverage tool flags the constructor, exclude it or use the generated coverage helper rather than reflection.
  3. In tests, assert the constructor is private via a reflection test that expects UnsupportedOperationException rather than trying to succeed.
  4. Remove any reflective newInstance() call against BarUtils.class.
Defensive patterns

Strategy: validation

Validate before calling

// Never instantiate; call static methods directly.
int h = BarUtils.getStatusBarHeight();

Try / catch

try {
    Constructor<BarUtils> c = BarUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException | InvocationTargetException e) {
    // expected: instantiation is forbidden by design
}

Prevention

When it happens

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

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

Related errors


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