LocalVariableTable attribute
The names and types of local variables, by slot and by range of code.
- Appears on
- Code
- Since
- class file 45.0 (Java 1.0)
- javac writes it
- with -g or -g:vars only
- Needed by the JVM
- no: it is for the class libraries (reflection), compilers or tools
Layout
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "LocalVariableTable" |
u4 | attribute_length | bytes after this field |
u2 | local_variable_table_length | |
{u2,u2,u2,u2,u2}[…] | local_variable_table | start_pc, length, name_index, descriptor_index, index |
What it is for
The names and types of local variables, by slot and by range of code. Debuggers need it to show variables by name. javac leaves it out by default, which is why a class compiled without -g shows arg0 and local variable slots instead of names.
Parameter names for reflection come from MethodParameters, not from here.
How common it is
JDK 25.0.5's java.base has 57,952 LocalVariableTable attributes, in 6,812 of its 7,391 classes (57,952 on Code attributes).
A real one, byte by byte
From Hello.class (compiled by javac 25), on method <init>, in its Code attribute:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 905 | 18 bytes | LocalVariableTable | |
| 905 | 00 1d | attribute_name_index | #29 |
| 907 | 00 00 00 0c | attribute_length | 12 |
| 911 | 00 01 | table_length | 1 |
| 913 | 10 bytes | variable 0 | this (slot 0) |
| 913 | 00 00 | start_pc | 0 |
| 915 | 00 05 | length | 5 |
| 917 | 00 1e | name_index | #30 |
| 919 | 00 1f | descriptor_index | #31 |
| 921 | 00 00 | index | 0 |
Specification: JVMS §4.7, The LocalVariableTable Attribute (Java SE 25).