33

I am thinking to build a VERY large Java class, is there any limit on the number of methods the Java class can have? Can it go into the millions of methods?

update: The purpose is, yes, to make a "God" class.

Phil
  • 46,436
  • 33
  • 110
  • 175
  • 7
    What is the purpose behind having a huge class with lots of methods? – AlwaysAProgrammer Dec 03 '10 at 03:04
  • 3
    Is there a strong reason for not wanting to refactor your large class? Your description has a suggestion of being a possible god-class anti-pattern. – Hovercraft Full Of Eels Dec 03 '10 at 03:05
  • 1
    Yeah I'm pretty interested in what it is you'll be doing. – Chuck Callebs Dec 03 '10 at 03:05
  • 2
    @Cameron - The point i was trying to make was about OO Design and maintainability of such an approach. It applies to auto generated code just as much. Also, any code, even if auto - generated has possibility of being debugged into by someone at some time. Your point about the vaguely threatening part noted. And comment removed. – Jagmag Dec 03 '10 at 03:39
  • Mixing and losing class structures in favor of god class for some type of code obfuscation? – Oleksii Kyslytsyn Jun 28 '17 at 08:27

3 Answers3

60

According to the Java class file specification the limit is 65535:

4.10 Limitations of the Java Virtual Machine

The following limitations of the Java virtual machine are implicit in the class file format:

  • The number of methods that may be declared by a class or interface is limited to 65535 by the size of the methods_count item of the ClassFile structure (§4.1). Note that the value of the methods_count item of the ClassFile structure does not include methods that are inherited from superclasses or superinterfaces.
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 4
    I think that since the method names also go to the constant pool, that 65535 limit is also shared with the names of other things in the class (such as constants, fields). – Thilo Dec 03 '10 at 03:06
  • 1
    Beware you may run into other limits first. I was trying to reveng a Hibernate model for a table with 750 fields. That is well under the 65535 method limit, unfortunately the class constructor can only take 255 parameters, and that killed my experiment right there. – Alex R May 16 '15 at 20:01
16

No. Some relevant pieces from the class file format spec:

The following limitations of the Java virtual machine are implicit in the class file format:

  • The per-class or per-interface constant pool is limited to 65535 entries by the 16-bit constant_pool_count field of the ClassFile structure (§4.1). This acts as an internal limit on the total complexity of a single class or interface.

  • The number of methods that may be declared by a class or interface is limited to 65535 by the size of the methods_count item of the ClassFile structure (§4.1). Note that the value of the methods_count item of the ClassFile structure does not include methods that are inherited from superclasses or superinterfaces.

I think this means that you can have 65535 methods, but only if you have no other objects that take up slots in the constant pool (field names for example).

In addition to that, there is also a maximum size for each method:

  • The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Neither method names nor type descriptors have to be unique, only the combination of both. E.g. you can combine 256 unique method names with 256 unique type descriptors to form 65536 unique method signatures, while only needing 512 constant pool entries. That’s far away from hitting the constant pool limitations. Also, a field and some methods can have the same name, sharing an entry. But it’s worth mentioning that *constructors* count like methods, reducing the possible number of declared methods. – Holger Jan 03 '18 at 14:22
0

Although methods_count in VM Spec is U2 and hence 65535 the format of a method_info has a name_index and a descriptor_index both of which must point into the constant pool which also has a U2 constant_pool_count so 32767 is the max, even this is of course not possible as it does not allow for any other entries for class name, super class fields etc.

  • 3
    Not exactly. You can have methods with the same name and different parameter types and you can have methods with different names and the same parameter types. So you can combine 256 unique method names with 256 unique type descriptors to form 65536 unique method signatures, while only needing 512 constant pool entries. – Holger Jan 03 '18 at 14:16