InnerClasses attribute
Nested classes did not exist in Java 1.0, and the JVM still knows nothing about them: Outer$Inner is an ordinary top-level class to the JVM.
- Appears on
- class
- Since
- class file 45.0 (Java 1.1)
- javac writes it
- for every class that is or refers to a nested class
- 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: "InnerClasses" |
u4 | attribute_length | bytes after this field |
u2 | number_of_classes | |
{u2,u2,u2,u2}[…] | classes | inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags |
What it is for
Nested classes did not exist in Java 1.0, and the JVM still knows nothing about them: Outer$Inner is an ordinary top-level class to the JVM. This attribute records the source-level relationship for compilers and reflection: the outer class, the simple name, and the flags as declared (private, static and protected, which a top-level class cannot have).
Every class that mentions a nested class lists it, not only the outer class, so the compiler can tell Outer$Inner from a class whose name merely contains a dollar sign.
How common it is
JDK 25.0.5's java.base has 5,544 InnerClasses attributes, in 5,544 of its 7,391 classes (5,544 on classs).
A real one, byte by byte
From Hello.class (compiled by javac 25), on the class:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1061 | 16 bytes | InnerClasses | |
| 1061 | 00 34 | attribute_name_index | #52 |
| 1063 | 00 00 00 0a | attribute_length | 10 |
| 1067 | 00 01 | number_of_classes | 1 |
| 1069 | 8 bytes | class 0 | java/lang/invoke/MethodHandles$Lookup |
| 1069 | 00 35 | inner_class_info_index | #53 |
| 1071 | 00 37 | outer_class_info_index | #55 |
| 1073 | 00 39 | inner_name_index | #57 |
| 1075 | 00 19 | inner_class_access_flags | 0x0019 public static final |
Specification: JVMS §4.7, The InnerClasses Attribute (Java SE 25).