Maximum Length for a Java Class Name ?

Discussion in 'Java' started by MattC, Mar 19, 2007.

  1. MattC

    MattC Guest

    Can anyone tell me the maximum length of a Java class name?

    Thanks,
    MattC
     
    MattC, Mar 19, 2007
    #1
    1. Advertising

  2. MattC

    Alex Hunsley Guest

    MattC wrote:
    > Can anyone tell me the maximum length of a Java class name?


    Have you tried the JLS (Java Language Specification), Google, and Google
    groups yet?
     
    Alex Hunsley, Mar 19, 2007
    #2
    1. Advertising

  3. MattC wrote:
    > Can anyone tell me the maximum length of a Java class name?


    Public classes needs to be in a file with the same name
    as the class, so the file system will probably limit
    it to 255 or something like that.

    BTW, you other programmers on the team will murder you if
    you use classnames longer than about 20 anyway ...

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Mar 19, 2007
    #3
  4. MattC

    Piotr Kobzda Guest

    MattC wrote:
    > Can anyone tell me the maximum length of a Java class name?


    For current JVMs it is a maximum of 65536 characters for fully qualified
    class name.

    However, not all such long names are allowed. The limit is 65536 bytes
    for fully qualified class name encoded using modified UTF-8 as described
    in "The class File Format" spec. It comes from 'u2' capacity (16 bits)
    -- the type of 'length' from constant pool 'CONSTANT_Utf8_info'
    structure used to store class names in class files.


    piotr
     
    Piotr Kobzda, Mar 19, 2007
    #4
  5. MattC

    Chris Uppal Guest

    Piotr Kobzda wrote:

    > For current JVMs it is a maximum of 65536 characters for fully qualified
    > class name.


    Occurs to me that there's a nice little obfuscation technique there -- rename
    all classes to so that their names start with, say, about 10 thousand spaces,
    and there aren't too mamy bytecode tools that will be usuable with the
    resulting classfiles ;-)

    -- chris
     
    Chris Uppal, Mar 19, 2007
    #5
  6. MattC

    Piotr Kobzda Guest

    Chris Uppal wrote:

    > Occurs to me that there's a nice little obfuscation technique there -- rename
    > all classes to so that their names start with, say, about 10 thousand spaces,
    > and there aren't too mamy bytecode tools that will be usuable with the
    > resulting classfiles ;-)


    Well, I think the bytecode verifier will prevent against spaces in
    names, but with other char, say 'x', or simply with randomly generated
    long name, why not?... ;-) Hopeless, it is very easy to rename
    classes/methods/fields in class-files, so in a case, when you really
    need good obfuscation, let more sophisticated tools do theirs job...
    Well, I can't recommend any tool of such type, but I've heard they
    exist... :) (In fact, I don't recall any need for such a tool from my
    past... Of course, I see reasons for it, but I know also, what such a
    tool can, and what it can't do... So I probably will never "destroy" my
    classes with it, there are lot of nicer things to do with Java classes
    than obfuscating... :) ).


    piotr
     
    Piotr Kobzda, Mar 20, 2007
    #6
  7. MattC

    Joe Attardi Guest

    On Mar 19, 10:25 am, "MattC" <> wrote:
    > Can anyone tell me the maximum length of a Java class name?


    If you have to ask, your class name is probably too long. :)
     
    Joe Attardi, Mar 20, 2007
    #7
  8. MattC

    Piotr Kobzda Guest

    Me:

    > Well, I think the bytecode verifier will prevent against spaces in
    > names, but with other char, say 'x', or simply with randomly generated
    > long name, why not?... ;-)


    Checked that, and it appears to me now, that, as Chris suggested, spaces
    are also allowed by recent class verifiers (even with -Xfuture option
    classes with only spaces in names are accepted).

    I tried to find any formal specification on verification which states
    that, but I haven't found anything stating it directly, neither in JLS,
    nor in JVMS, except a single non-normative fragment from the latter
    concerning verifications performed by the verifier:

    "• Checking that all field references and method references in the
    constant pool have valid names, valid classes, and a valid type descriptor."

    It is not about class names, it's on method and field references names
    only, and even for them, it is not clear to me what a "valid names"
    means here?...

    So, it's better to assume, that on JVM level, any class name (excluding
    maybe empty, and incorrectly encoded one) is allowed.


    P.S.
    There is also a mistake in my advice to the OP, the maximum class name
    length is 65535 (not 65536) characters, sorry for that.


    piotr
     
    Piotr Kobzda, Mar 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,965
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  2. Replies:
    2
    Views:
    5,935
  3. harry
    Replies:
    11
    Views:
    954
    Roedy Green
    Feb 1, 2006
  4. David T. Ashley

    Maximum Function / Variable Name Identifier Length?

    David T. Ashley, Jan 2, 2007, in forum: C Programming
    Replies:
    3
    Views:
    656
    aegis
    Jan 2, 2007
  5. phanhuyich
    Replies:
    4
    Views:
    278
Loading...

Share This Page