What javac does
Inner classes and nestmates
access$000 methods up to Java 10; direct access between nestmates since Java 11.
To the JVM there are no inner classes: Nestmates$Peek is a separate class, and until Java 11 it could not touch Nestmates' private members. So javac generated package-private static accessor methods, access$000 and access$100, in the outer class and called those.
Java 11 introduced nestmates (JEP 181): classes in the same nest may use each other's private members. javac now emits a plain getfield and invokestatic, and records the relationship in the NestHost and NestMembers attributes.
The source
// Inner classes and private members: accessor methods before Java 11, nestmates after.
public class Nestmates {
private int secret = 42;
private static String hidden() {
return "hidden";
}
class Peek {
int look() {
return secret;
}
String call() {
return hidden();
}
}
}Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.
What to look for
- With
--release 8,look()callsNestmates.access$000(this$0); from--release 11on it readssecretdirectly withgetfield. - The inner class keeps its outer instance in a synthetic final field,
this$0, which the constructor sets before it even calls the superclass constructor. - With
--release 25, javac also checks the outer instance withObjects.requireNonNullas the constructor stores it.
Nestmates.class
private static String hidden()
javac --release 8 to 25 (the same bytecode for every release)
static int access$000(Nestmates x0) synthetic
Not present with --release 11 to 25.
javac --release 8
static String access$100() synthetic
Not present with --release 11 to 25.
javac --release 8
Nestmates$Peek.class
A separate class file, Nestmates$Peek.class, that javac generated from the same source.
Peek(Nestmates)
int look()
javac --release 8
return secret;javac --release 11 to 25
String call()
javac --release 8
javac --release 11 to 25