38

This question came up in Spring class, which has some rather long class names. Is there a limit in the language for class name lengths?

Allan Bowe
  • 11,975
  • 19
  • 66
  • 121
Eugene Katz
  • 5,198
  • 7
  • 38
  • 49

4 Answers4

51

The Java Language Specification states that identifiers are unlimited in length.

In practice though, the filesystem will limit the length of the resulting file name.

chepseskaf
  • 654
  • 2
  • 12
  • 39
Robert Munteanu
  • 65,183
  • 33
  • 198
  • 275
33

65535 characters I believe. From the Java virtual machine specification:

The length of field and method names, field and method descriptors, and other constant string values is limited to 65535 characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (§4.4.7). Note that the limit is on the number of bytes in the encoding and not on the number of encoded characters. UTF-8 encodes some characters using two or three bytes. Thus, strings incorporating multibyte characters are further constrained.

here:

https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html#88659

valiano
  • 13,473
  • 5
  • 50
  • 72
Jon
  • 59,288
  • 31
  • 123
  • 150
3

With JDK 1.5, the practical limit for class names on Windows XP with 255 -- longer names gave errors in the file system. This was the full name (directory+package+class).

I have not tried JDK 1.6 on Vista or windows 7, hopefully Sun fixed it to be the NTFS limit of 8000 or so.

Stanleymit
  • 31
  • 1
0

No. Java doesn't impose any limit on the class name. But if you interfacing with other systems (e.g. JNI) its better to be on the safe side.

lud0h
  • 2,350
  • 6
  • 33
  • 41