JessYanCoding/AndroidAutoSize · error · java.lang.IllegalStateException

you can't instantiate me!

Error message

you can't instantiate me!

What it means

This is a deliberate sentinel guard, not a runtime failure of library code. AutoSizeLog is a static-only utility class (all members static), so its constructor is private; any attempt to instantiate it via `new AutoSizeLog()` (or via reflection, if setAccessible bypasses privacy) throws this IllegalStateException immediately. The input at fault is the instantiation attempt itself; correct usage is to call the static methods (e.g. AutoSizeLog.d/w/e) directly.

Solutions

  1. Use the static methods, e.g. AutoSizeLog.debug(...), without creating an instance
  2. Remove the instantiation code
  3. Exclude static utility classes from auto-instantiation in DI/codegen tooling

Example fix

// before
AutoSizeLog log = new AutoSizeLog();
// after
AutoSizeLog.debug(TAG, "adaptation done");
Defensive patterns

Strategy: try-catch

Validate before calling

if (Modifier.isPrivate(AutoSizeLog.class.getDeclaredConstructor().getModifiers())) { /* logging helper: use statics */ }

Type guard

boolean isUtilityClass(Class<?> c) { return Modifier.isPrivate(c.getDeclaredConstructors()[0].getModifiers()); }

Try / catch

try { new AutoSizeLog(); } catch (IllegalStateException e) { /* expected: use AutoSizeLog.debug/warn/error statically */ }

Prevention

When it happens

Trigger: Calling new AutoSizeLog() or letting a framework instantiate it.

Common situations: Reflection-based instantiation in tests or DI containers; developers treating the logging helper as an injectable logger object.

Related errors


AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07). Data as JSON: /api/errors/80aec0159bf3d191. Report an issue: GitHub.

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/utils/AutoSizeLog.java:32

 * limitations under the License.
 */
package me.jessyan.autosize.utils;

import android.util.Log;

/**
 * ================================================
 * Created by JessYan on 2018/8/8 18:48
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class AutoSizeLog {
    private static final String TAG = "AndroidAutoSize";
    private static boolean debug;

    private AutoSizeLog() {
        throw new IllegalStateException("you can't instantiate me!");
    }

    public static boolean isDebug() {
        return debug;
    }

    public static void setDebug(boolean debug) {
        AutoSizeLog.debug = debug;
    }

    public static void d(String message) {
        if (debug) {
            Log.d(TAG, message);
        }
    }

    public static void w(String message) {
        if (debug) {

View on GitHub (pinned to e402ecdd99)