• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

maximum length of method names  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering : is there a maximum in the length of the method names ? Or more precisely : is ther a maxiumum in the significant number of characters in a name ?
Like : getThis and getThat are both allowed, but because the JVM only looks at the first 5 characters, both names are equal.

Thanks

Henk
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I think the only limitation is that the method name will be stored in the class file as a CONSTANT_Utf8_info structure, which uses an unsigned two-byte integer to represent the number of bytes in the String data. This limits the length of a method name to around 65536 characters. I'd be very impressed -- or horrified -- if anyone had hit this limitation in real life!
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is 65535 characters to be exact. The same rule applies to the length of the class level variables as well (but not to the local variables - the one which is defined inside a method). Hope you can figure out why.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just said "around 2^16" because the limit may actually be less if any of the characters are outside of the 7-bit ASCII range, because then multiple bytes will be used per character.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops..forgot that. You are right Ernest
 
Henk O
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

thanks guys,

just to be sure : although the length is almost unlimited, does the compiler/jvm use every character. It's just that I know in C, a functionname can have almost any length, but only the first, say, 14 characters are actually used.

Henk
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a limitation of old-fashioned linkers, generally not a compiler or a language itself. It's not really a problem anymore -- C++'s long mangled names forced linker developers to accomodate arbitrarily long external symbols.

In any case, Java doesn't use the platform linker to link its classes, so no such limitation applies. The answer is yes, every character counts.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Henk,

welcome at the Ranch!

You probably missed that our naming policy requires everyone to use a full last name. Please adjust your display name accordingly. Thanks!

Regarding your question, I'm quite sure that Java uses the full name to differentiate between identifiers, no matter how long they are.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!