Maximum number of parameters in Java method declaration

What is the maximum number of parameters that a method in Java can have and why?

I am using Java 1.8 on a 64-bit Windows system.

All the answers on StackOverflow about this say that the technical limit is 255 parameters without specifying why.

To be precise, 255 for static and 254 for non-static (this will be the 255th in this case) methods.

I thought this could be described in some sort of specification and that there is simply a statically defined maximum number of parameters allowed.

But this was only valid for int and all 4-bytes types. I did some tests with long parameters, and I was only able to declare 127 parameters in that case.

With String parameters, the allowed number I deduced from testing is 255 (it may be because the reference size is 4 bytes in Java?).

But since I am using a 64-bit system, references size should be 8 bytes wide and so with String parameters the maximum allowed number should be 127, similar to long types.

How does this limit is exactly applied?

Does the limit have anything to do with the stack size of the method?

Note: I am not really going to use these many parameters in any method, but this question is only to clarify the exact behavior.


That limit is defined in the JVM Specification:

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.

Section §4.3.3 gives some additional information:

A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations.

The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

Your observations were spot on, double word primitives (long/double) need twice the size of usual 4 bytes variables and 4 bytes object instance references.

Regarding the last part of your question related to 64bit systems, the specification defines how many units a parameter contribute, that part of the specification must still be complied with even on a 64bit platform, the 64bit JVM will accomodate 255 instance parameters (like your 255 Strings) regardless of the internal object's pointer size.


Section 4.3.3 of the JVM specification has the information you are looking for:

A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations. The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

Therefore it appears that whether the host-machine is 32-bit or 64-bit has no effect on the number of parameters. If you notice, the documentation speaks in terms of "units", where the length of one "unit" is a function of the word-size. If the number of parameters directly proportional to word-size, there would be portability issues; you wouldn't be able to compile the same Java program on different architectures (assuming that at least one method used the maximum-number of parameters on the architecture with the larger word-size).


I found an interesting issue from a newsletter about this, http://www.javaspecialists.eu/archive/Issue059.html

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. This acts as an internal limit on the total complexity of a single class or interface. 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, in the LineNumberTable attribute, and in the LocalVariableTable attribute.

The greatest number of local variables in the local variables array of a frame created upon invocation of a method is limited to 65535 by the size of the max_locals item of the Code attribute giving the code of the method. Note that values of type long and double are each considered to reserve two local variables and contribute two units toward the max_locals value, so use of local variables of those types further reduces this limit.

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. Note that the value of the fields_count item of the ClassFile structure does not include fields that are inherited from superclasses or superinterfaces.