[英]What is the maximum number of methods a Java class can have?
I am thinking to build a VERY large Java class, is there any limit on the number of methods the Java class can have? 我正在考虑构建一个非常大的Java类,Java类可以有多少方法限制? Can it go into the millions of methods?
它可以进入数以百万计的方法吗?
update: The purpose is, yes, to make a "God" class. 更新:目的是,做一个“上帝”课程。
According to the Java class file specification the limit is 65535 : 根据Java类文件规范 ,限制为65535 :
4.10 Limitations of the Java Virtual Machine
4.10 Java虚拟机的限制
The following limitations of the Java virtual machine are implicit in the
class
file format:Java虚拟机的以下限制隐含在
class
文件格式中:
- 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 theClassFile
structure ( §4.1 ).可以由类或接口声明的方法数量被
ClassFile
结构(第4.1节 )的methods_count
项的大小限制为65535。 Note that the value of themethods_count
item of theClassFile
structure does not include methods that are inherited from superclasses or superinterfaces.请注意,
ClassFile
结构的methods_count
项的值不包括从超类或超接口继承的方法。
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). ClassFile结构的16位constant_pool_count字段(第4.1节)将每类或每接口常量池限制为65535个条目。 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). 可以由类或接口声明的方法数量被ClassFile结构(第4.1节)的methods_count项的大小限制为65535。 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结构的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: 除此之外,每种方法还有最大尺寸:
虽然VM Spec中的methods_count是U2,因此65535,method_info的格式有一个name_index和一个descriptor_index,两者都必须指向常量池,它也有一个U2 constant_pool_count所以32767是最大值,即使这当然也不可能它不允许任何其他类名,超类字段等条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.