What is the maximum number of local variables allowed in a Java method (per JVM spec)?

The story behind the answer

The maximum number of local-variable slots in a Java method is 65535 under the JVM specification.

The limit comes from the Code attribute’s 16-bit max_locals field, which records the size of a method frame’s local-variable array. It includes slots used for method parameters as well as variables declared inside the method. The JVM instruction set also uses 16-bit local-variable indexing, reinforcing the same ceiling.

This is a limit on slots, not necessarily on the number of variable declarations. Values of type long and double each consume two slots, while most other values consume one. An instance method also uses one slot for its implicit this reference. Consequently, a real method may be unable to declare 65535 separate variables, especially when parameters, wide primitive types, and the receiver are counted.

The limit is distinct from the operand-stack limit: max_stack can also reach 65535, but it measures stack values rather than local-variable slots. Java source compilers and bytecode tools may impose additional practical limits before the JVM ceiling is reached.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: