Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
ScreenUtils is a final utility class whose only constructor is private and throws UnsupportedOperationException. The class exposes only static methods for screen dimensions/orientation, so it must never be instantiated. The throw is a guard: it fires the moment anything bypasses compile-time access control and invokes the constructor via reflection.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java:35
import android.view.Surface;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import static android.Manifest.permission.WRITE_SETTINGS;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/02
* desc : utils about screen
* </pre>
*/
public final class ScreenUtils {
private ScreenUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return the width of screen, in pixel.
*
* @return the width of screen, in pixel
*/
public static int getScreenWidth() {
WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.x;
}View on GitHub (pinned to 7b4caf9e54)
Solutions
- Do not instantiate ScreenUtils — call its static methods directly, e.g. ScreenUtils.getScreenWidth().
- If a serialization framework tries to build it, add a TypeAdapter/InstanceCreator (Gson) or a mixin (Jackson) that returns null or a sentinel rather than invoking the constructor.
- If a mock framework is the culprit, configure it with Mockito.mockStatic (Mockito 3.4+) or use PowerMock on the static method rather than constructing the class.
- If you genuinely need an instance, subclass or wrap the static API behind your own injectable component instead of breaking the constructor.
Example fix
// before (Gson tries to construct the field type)
public class Config { ScreenUtils screen; }
new Gson().fromJson(json, Config.class);
// after
public class Config { int screenWidth; }
int w = ScreenUtils.getScreenWidth(); Defensive patterns
Strategy: validation
Validate before calling
// Before any reflective construction, check it is not a static-only utility.
if (Modifier.isFinal(ScreenUtils.class.getModifiers())
&& ScreenUtils.class.getDeclaredConstructors()[0].getParameterCount() == 0) {
throw new IllegalStateException("ScreenUtils is a static utility; do not instantiate.");
} Prevention
- Never write code that instantiates utility classes; treat final + private-constructor as a hard contract.
- Configure Gson/Jackson with InstanceCreator/mixins for any utility-typed fields.
- Use Mockito.mockStatic for static APIs instead of constructing the class.
- Add a static-analysis check (ErrorProne/ForbiddenApi) banning getDeclaredConstructor on util classes.
When it happens
Trigger: Triggered when code calls ScreenUtils.class.getDeclaredConstructor().setAccessible(true).newInstance() (directly or transitively). Common transitive causes: Gson/Jackson deserializing into a ScreenUtils field, Mockito/PowerMock attempting to instantiate the class for mocking, Kotlin reflection, or a DI container scanning the classpath.
Common situations: Unit tests where a test runner or mock framework tries to construct utility classes; JSON deserialization that maps a field to ScreenUtils; accidental `new ScreenUtils()` after removing the final keyword in a fork; code-coverage tools (JaCoCo) generating reflective probes.
Related errors
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/a3694d4bf118d6f7.
Report an issue: GitHub.