Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
NumberUtils is a utility class providing static methods for number formatting (format, int2Binary, binary2Int, etc.) with a ThreadLocal DecimalFormat for thread safety. Its private constructor throws UnsupportedOperationException to enforce static-only access.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/NumberUtils.java:30
* time : 2020/04/12
* desc : utils about number
* </pre>
*/
public final class NumberUtils {
private static final ThreadLocal<DecimalFormat> DF_THREAD_LOCAL = new ThreadLocal<DecimalFormat>() {
@Override
protected DecimalFormat initialValue() {
return (DecimalFormat) NumberFormat.getInstance();
}
};
public static DecimalFormat getSafeDecimalFormat() {
return DF_THREAD_LOCAL.get();
}
private NumberUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Format the value.
*
* @param value The value.
* @param fractionDigits The number of digits allowed in the fraction portion of value.
* @return the format value
*/
public static String format(float value, int fractionDigits) {
return format(value, false, 1, fractionDigits, true);
}
/**
* Format the value.
*
* @param value The value.
* @param fractionDigits The number of digits allowed in the fraction portion of value.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Use static methods: NumberUtils.format(value, fractionDigits), NumberUtils.getSafeDecimalFormat(). Never call new NumberUtils().
- Exclude utility classes from DI scanning and serialization configs.
- Mock static methods with mockito-inline for testing.
Example fix
// before NumberUtils nu = new NumberUtils(); String formatted = nu.format(3.14159f, 2); // after String formatted = NumberUtils.format(3.14159f, 2);
Defensive patterns
Strategy: type-guard
Validate before calling
// NumberUtils is static-only String formatted = NumberUtils.format(3.14159, 2); DecimalFormat df = NumberUtils.getSafeDecimalFormat();
Type guard
static boolean isStaticUtility(Class<?> c) {
return c.getSimpleName().endsWith("Utils");
} Try / catch
try {
NumberUtils.class.getDeclaredConstructor().setAccessible(true);
NumberUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
// Use static methods
} Prevention
- Never instantiate classes ending in 'Utils'.
- Exclude util packages from DI and serialization scanning.
- Use NumberUtils.getSafeDecimalFormat() for thread-local DecimalFormat.
When it happens
Trigger: Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection.
Common situations: Component scanning in DI traverses the utility package; a test framework instantiates all classes for coverage; a serialization library targets the class.
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/1ddab640ab5b6a37.
Report an issue: GitHub.