EnclosingMethod attribute
For a local or anonymous class, the class and method it is written in.
- Appears on
- class
- Since
- class file 49.0 (Java 5)
- javac writes it
- for local and anonymous classes
- 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: "EnclosingMethod" |
u4 | attribute_length | bytes after this field |
u2 | class_index | the enclosing class |
u2 | method_index | a NameAndType for the enclosing method, or 0 if in an initialiser |
What it is for
For a local or anonymous class, the class and method it is written in. Class.getEnclosingMethod() reads it.
How common it is
JDK 25.0.5's java.base has 689 EnclosingMethod attributes, in 689 of its 7,391 classes (689 on classs).
A real one, byte by byte
From EnumSwitch$1.class (compiled by javac 25), on the class:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 611 | 10 bytes | EnclosingMethod | |
| 611 | 00 24 | attribute_name_index | #36 |
| 613 | 00 00 00 04 | attribute_length | 4 |
| 617 | 00 25 | class_index | #37 |
| 619 | 00 00 | method_index | #0 |
Specification: JVMS §4.7, The EnclosingMethod Attribute (Java SE 25).