LineNumberTable attribute
Maps bytecode offsets to source lines, for stack traces and debuggers.
- Appears on
- Code
- Since
- class file 45.0 (Java 1.0)
- javac writes it
- by default (not with -g:none)
- 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: "LineNumberTable" |
u4 | attribute_length | bytes after this field |
u2 | line_number_table_length | |
{u2,u2}[…] | line_number_table | start_pc, line_number |
What it is for
Maps bytecode offsets to source lines, for stack traces and debuggers. The entries mark where a line's code starts; a line can have several entries if its code is split up, as loop conditions are.
How common it is
JDK 25.0.5's java.base has 61,100 LineNumberTable attributes, in 6,827 of its 7,391 classes (61,100 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 |
|---|---|---|---|
| 893 | 12 bytes | LineNumberTable | |
| 893 | 00 1c | attribute_name_index | #28 |
| 895 | 00 00 00 06 | attribute_length | 6 |
| 899 | 00 01 | line_number_table_length | 1 |
| 901 | 4 bytes | line 0 | line 1 from @0 |
| 901 | 00 00 | start_pc | 0 |
| 903 | 00 01 | line_number | 1 |
Specification: JVMS §4.7, The LineNumberTable Attribute (Java SE 25).