JessYanCoding/AndroidAutoSize · error · java.lang.IllegalStateException
you can't instantiate me!
Error message
you can't instantiate me!
What it means
ScreenUtils is a static utility class whose constructor is private and deliberately throws IllegalStateException("you can't instantiate me!"). Instantiating it (new ScreenUtils()) is a misuse; all methods like getStatusBarHeight() are static. The exception exists purely to enforce the utility-class pattern.
Solutions
- Use the static methods directly: ScreenUtils.getStatusBarHeight(context) — remove the 'new'.
- Exclude utility classes from DI/reflection classpath scanning.
- In tests, do not instantiate; call the static methods instead.
Example fix
// before ScreenUtils utils = new ScreenUtils(); int h = utils.getStatusBarHeight(); // after int h = ScreenUtils.getStatusBarHeight();
Defensive patterns
Strategy: type-guard
Validate before calling
public static boolean isUtilityClass(Class<?> clazz) {
try {
java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();
c.setAccessible(true);
return false; // instantiation attempt will throw for guarded utilities
} catch (Exception e) {
return true;
}
}
// Simply avoid 'new ScreenUtils()': check Modifier.isStatic usage patterns in review. Type guard
public static boolean isInstantiable(Class<?> clazz) {
int mods = clazz.getModifiers();
return !java.lang.reflect.Modifier.isAbstract(mods)
&& !clazz.getName().endsWith("Utils")
&& !clazz.getSimpleName().matches(".*(Utils|Util|Preconditions|ScreenUtils)");
} Try / catch
try {
ScreenUtils.class.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException e) {
// expected: constructor throws IllegalStateException
Log.w("Reflect", "ScreenUtils is a static utility class; use static methods", e);
} Prevention
- Call ScreenUtils.getStatusBarHeight() and other helpers statically; never construct the class.
- Configure DI frameworks (Dagger/Spring-style scanners) to skip *Utils classes.
- Keep the convention that classes named *Utils are static-only and un-instantiable.
- When using reflection generically, filter out private constructors or catch IllegalStateException from the constructor.
When it happens
Trigger: Writing new ScreenUtils() anywhere — directly in app code, via reflection (Class.newInstance / getDeclaredConstructor().newInstance()), or a dependency-injection framework attempting to construct it.
Common situations: Copy-paste habits from non-utility classes, DI frameworks scanning and trying to instantiate every class in a package, or reflection-based test scaffolding.
Related errors
- Not in applications main thread
- you can't instantiate me!
- you can't instantiate me!
- you should init first
- negative size:
AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07).
Data as JSON: /api/errors/ddc9c943802a07ff.
Report an issue: GitHub.
Appendix: source
Thrown at autosize/src/main/java/me/jessyan/autosize/utils/ScreenUtils.java:37
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
/**
* ================================================
* Created by JessYan on 26/09/2016 16:59
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public class ScreenUtils {
private ScreenUtils() {
throw new IllegalStateException("you can't instantiate me!");
}
public static int getStatusBarHeight() {
int result = 0;
try {
int resourceId = Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = Resources.getSystem().getDimensionPixelSize(resourceId);
}
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取当前的屏幕尺寸
*View on GitHub (pinned to e402ecdd99)