Is there a max length for class/method/variable names in Java? the JLS doesn't seem to mention that. I know very long names are problematic anyway from code readability and maintainability perspective, but just out of curiosity is there a limitation (I guess class names might be limited by the file system maximal file name limitation).
3 Answers
If I'm not mistaken, the limit is not in the language itself but in the classfile format, which limits names to 64k, so for all practical intents and purposes identifier length is not a problem. Specifically, this is the definition of a constant string in the pool, which seems to imply the maximal length is 16 bit:
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
Class names may be more of an issue for file systems, I agree, I'm not sure what's currently supported.

- 88,451
- 51
- 221
- 321
-
No, because the question was about Java-the-language, not some implementation of a compiler for that language, even if that compiler happens to be the reference implementation. – Sebastian Graf Nov 13 '18 at 13:38
Also found similar question (though it didn't appear in my initial search, or when I typed the question title which is weird): Maximum Method Name Length
-
2
-
1Which is actually fairly surprising, considering that the language was meant to compile for the classfiles... – Uri Mar 30 '09 at 04:00
-
1true, I wonder what happens if you actually try to create a ridiculously long variable name – talg Mar 30 '09 at 04:31
-
4Main.java:1: UTF8 representation for string "aaaaaaaaaaaaaaaaaaaa..." is too long for the constant pool – user471011 Dec 05 '10 at 17:22
If you go over the size limit imposed by the VM for method names then you get a compiler error (at least with the version of javac I am using):
Main.java:1: UTF8 representation for string "aaaaaaaaaaaaaaaaaaaa..." is too long for the constant pool