{"record":{"id":"451d28e76419783a","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-451d28","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"info","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/EncodeUtils.java","lineNumber":22,"sourceCode":"import android.text.Html;\nimport android.util.Base64;\n\nimport java.io.UnsupportedEncodingException;\nimport java.net.URLDecoder;\nimport java.net.URLEncoder;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/07\n *     desc  : utils about encode\n * </pre>\n */\npublic final class EncodeUtils {\n\n    private EncodeUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return the urlencoded string.\n     *\n     * @param input The input.\n     * @return the urlencoded string\n     */\n    public static String urlEncode(final String input) {\n        return urlEncode(input, \"UTF-8\");\n    }\n\n    /**\n     * Return the urlencoded string.\n     *\n     * @param input       The input.\n     * @param charsetName The name of charset.\n     * @return the urlencoded string","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/EncodeUtils.java#L4-L40","documentation":"EncodeUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). All encoding helpers (URL-encode, base64, html-encode, binary string) are static; the constructor exists only to prevent instantiation, consistent with the library-wide *Utils pattern.","triggerScenarios":"Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes from Class references. A direct `new EncodeUtils()` fails to compile due to the private constructor.","commonSituations":"Coverage/quality tools that instantiate all classes; DI or reflective object mappers; a developer who expects instance state; service loaders that scan packages.","solutions":["Use the static API: EncodeUtils.urlEncode(input), EncodeUtils.base64Encode(...), etc.","Add EncodeUtils to the exclusion list of any tool that reflectively instantiates classes.","For a contract test, expect UnsupportedOperationException from the reflected constructor."],"exampleFix":"// before (reflection)\nEncodeUtils eu = EncodeUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nString enc = EncodeUtils.urlEncode(\"a b\");","handlingStrategy":"validation","validationCode":"// Never construct EncodeUtils; it is static-only.\nif (clazz == EncodeUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use EncodeUtils.xxx() statically\n}","typeGuard":"if (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // static-only; do not newInstance()\n}","tryCatchPattern":"try {\n    Constructor<?> c = EncodeUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use EncodeUtils.xxx() instead\n}","preventionTips":["EncodeUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call EncodeUtils.urlEncode()/base64Encode()/binaryEncode() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","encode"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}