HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Inner classes and nestmates

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() calls Nestmates.access$000(this$0); from --release 11 on it reads secret directly with getfield.
  • 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 with Objects.requireNonNull as the constructor stores it.

Nestmates.class

private static String hidden()

javac --release 8 to 25 (the same bytecode for every release)

6return "hidden";
0ldc #13// "hidden"
2areturn

Open in the viewer

static int access$000(Nestmates x0) synthetic

Not present with --release 11 to 25.

javac --release 8

2public class Nestmates {
0aload_0 // x0
1getfield #7// Nestmates.secret : int
4ireturn

Open in the viewer

static String access$100() synthetic

Not present with --release 11 to 25.

javac --release 8

2public class Nestmates {
0invokestatic #1// Nestmates.hidden() : String
3areturn

Open in the viewer

Nestmates$Peek.class

A separate class file, Nestmates$Peek.class, that javac generated from the same source.

Peek(Nestmates)

javac --release 8 to 21

9class Peek {
0aload_0 // this
1aload_1 // this$0
2putfield #1// Nestmates$Peek.this$0 : Nestmates
5aload_0 // this
6invokespecial #7// new Object()
9return

Open in the viewer

javac --release 25

9class Peek {
0aload_0 // this
1aload_1 // this$0
2dup
3invokestatic #1// Objects.requireNonNull(Object) : Object
6pop
7putfield #7// Nestmates$Peek.this$0 : Nestmates
10aload_0 // this
11invokespecial #13// new Object()
14return

Open in the viewer

int look()

javac --release 8

11return secret;
0aload_0 // this
1getfield #1// Nestmates$Peek.this$0 : Nestmates
4invokestatic #13// Nestmates.access$000(Nestmates) : int
7ireturn

Open in the viewer

javac --release 11 to 25

11return secret;
0aload_0 // this
1getfield #7// Nestmates$Peek.this$0 : Nestmates
4getfield #19// Nestmates.secret : int
7ireturn

Open in the viewer

String call()

javac --release 8

15return hidden();
0invokestatic #19// Nestmates.access$100() : String
3areturn

Open in the viewer

javac --release 11 to 25

15return hidden();
0invokestatic #25// Nestmates.hidden() : String
3areturn

Open in the viewer

← synchronizedAll topicsRecords →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.