Reputation: 1851
Is there a limit to how many fields can be declared in a Java class? This question addresses the number of arguments of a method, but I am curious knowing if a Java class has any limit, and what is this limit.
This is just out of curiosity - I wouldn't actually declare a class with thousands of fields.
Upvotes: 2
Views: 404
Reputation: 3105
Yes, 65535. It is explained here https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.11
The relevant part is:
The number of fields that may be declared by a class or interface is limited to 65535 by the size of the fields_count item of the ClassFile structure (§4.1).
Note that the value of the fields_count item of the ClassFile structure does not include fields that are inherited from superclasses or superinterfaces.
Upvotes: 4
Reputation: 137309
Exactly 65535, not counting the inherited fields.
From Limitations of the Java Virtual Machine:
The number of fields that may be declared by a class or interface is limited to 65535 by the size of the
fields_count
item of theClassFile
structure (§4.1).Note that the value of the
fields_count
item of theClassFile
structure does not include fields that are inherited from superclasses or superinterfaces.
Upvotes: 8