Java 类可以拥有的最大方法数是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4342072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:57:34  来源:igfitidea点击:

What is the maximum number of methods a Java class can have?

javaclass

提问by Phil

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?

我正在考虑构建一个非常大的 Java 类,Java 类可以拥有的方法数量是否有任何限制?它可以进入数百万种方法吗?

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

更新: 是的,目的是制作“上帝”课程。

回答by John Kugelman

According to the Java class file specificationthe limit is 65535:

根据Java 类文件规范,限制为65535

4.10 Limitations of the Java Virtual Machine

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

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

4.10 Java 虚拟机的局限性

Java 虚拟机的以下限制隐含在class文件格式中:

  • 可以由类或接口声明的方法数量methods_countClassFile结构项的大小限制为 65535 (第4.1 节)。请注意,结构methods_count项的值ClassFile不包括从超类或超接口继承的方法。

回答by Thilo

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:

Java 虚拟机的以下限制隐含在类文件格式中:

  • 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.

  • 每个类或每个接口的常量池被 ClassFile 结构(第 4.1 节)的 16 位 constant_pool_count 字段限制为 65535 个条目。这是对单个类或接口的总复杂性的内部限制。

  • 类或接口可以声明的方法数被 ClassFile 结构的 methods_count 项的大小限制为 65535(第 4.1 节)。请注意,ClassFile 结构的methods_count 项的值不包括从超类或超接口继承的方法。

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).

我认为这意味着您可以拥有 65535 个方法,但前提是您没有其他对象占用常量池中的插槽(例如字段名称)。

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).
  • 每个非本机、非抽象方法的代码量限制为 65536 字节,这取决于 Code 属性(第 4.7.3 节)、LineNumberTable 属性(第 4.7.8 节)的 exception_table 中的索引大小,以及在 LocalVariableTable 属性中(第 4.7.9 节)。

回答by John Priest

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.

尽管VM Spec中的methods_count是U2,因此是65535,但是method_info的格式有name_index和descriptor_index,两者都必须指向也有U2 constant_pool_count的常量池,所以32767是最大值,即使这当然是不可能的它不允许为类名、超类字段等输入任何其他条目。