Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How do nested classes access each other's private members at the bytecode level, and what changed with nestmates in JDK 11?

The JVM originally treated every nested class as a separate class with no special rights, so to let Outer and Outer$Inner use each other's private members, javac generated package-private synthetic bridge methods such as access$000. JDK 11 (JEP 181) added nests: the NestHost and NestMembers class file attributes tell the JVM which classes form one nest, and the JVM allows private access among them directly. That removed the synthetic accessors, made bytecode smaller and made reflection agree with the language, and Class.getNestHost() and getNestMembers() expose nests at runtime.

Why this question is asked

Java has always let a nested class use the private members of its outer class, and the outer class use the private members of its nested classes. What many developers never noticed is that, for over twenty years, the JVM itself did not allow it, and javac quietly bridged the gap with generated methods. Interviewers ask about this to see whether we can connect language rules to what actually runs, and because the old bridges still appear in stack traces and profiles of old code. In this answer, we will recap why there was a gap, look at the generated accessors, see what nestmates changed in JDK 11, and see how reflection and tools reveal nests.

1A Quick Recap: The Language and the JVM Disagreed

In the Java language, private means private to the top-level class, including everything nested inside it. The JVM, however, does not know about nesting in source. After compilation, Outer and Outer.Inner become two separate class files, Outer.class and Outer$Inner.class, and to the JVM before JDK 11 they were just two classes in the same package. A private member was accessible only from code inside its own class file.

So when Inner read Outer's private field, the language said yes and the JVM would have said no. javac had to make the program work anyway, and it did so with generated code.

2Before JDK 11: Synthetic Accessor Methods

Take a simple pair of classes where the inner class reads a private field of the outer class.

Java
public class Outer {
private int secret = 42;
class Inner {
int peek() { return secret; } // reads Outer's private field
}
}
// javac 8 output, shown with javap -p -c:
// class Outer {
// private int secret;
// static int access$000(Outer x0); // synthetic, package-private
// getfield secret
// }
// class Outer$Inner {
// int peek(); calls Outer.access$000(this$0)
// }

Notice what javac did. It added a static method access$000 to Outer, with package access, that simply returns the private field. Inner calls that method instead of reading the field. The JVM is happy, because the call is from one class to a package-private method of another class in the same package.

This had three costs. Every such access added a method to the class file, and real code had dozens, which mattered for Android's method count limits and class size. The accessors widened access: any class in the same package could, in principle, call access$000, so private was not truly private at the bytecode level. And reflection followed JVM rules, not language rules, so Inner using reflection to read Outer's private field threw IllegalAccessException even though the same access in plain source compiled fine.

3JDK 11: Nests and Nest-Based Access

JEP 181 taught the JVM about nesting. A nest is a group of classes that came from one top-level class: the top-level class is the nest host, and every class nested inside it, at any depth, is a nest member. javac records this in two class file attributes. The host has a NestMembers attribute listing every member, and each member has a NestHost attribute naming the host.

Java
// javac 11+ output, javap -v:
// Outer.class: NestMembers: Outer$Inner
// Outer$Inner.class: NestHost: class Outer
//
// Outer$Inner.peek():
// aload_0
// getfield this$0 // the hidden outer reference
// getfield secret // direct access to Outer's private field
// ireturn
Step throughWhat a nest looks like to the JVM
  1. Nest host: OuterNestMembers attribute lists Outer$Inner and Outer$Inner$Deep
  2. Member: Outer$InnerNestHost attribute points to Outer
  3. Member: Outer$Inner$DeepNestHost also points to Outer, the top-level class
  4. Access checkprivate access allowed when both classes share the same validated host

4 layers, from the top down. Step through them one at a time.

Every class in the nest names the same top-level host, and the host lists every member.

When the JVM checks access to a private member, it now also allows the access if both classes belong to the same nest. To confirm that, it checks that the member's NestHost attribute and the host's NestMembers list agree, so a class cannot simply claim to belong to someone else's nest. With that rule in place, javac 11 and later emit direct getfield, putfield and method calls, and the access$ methods disappear.

4What Changed in Practice

Class files became smaller and simpler, and stack traces stopped showing access$000 frames between our methods. Private methods called from nestmates are now invoked directly; JDK 11 also allowed invokevirtual and invokeinterface on private methods, which javac uses for these calls.

Reflection was brought into line with the language. Since the reflection access check uses the same nest rule, Inner can now call Outer's private method through reflection without setAccessible, just as it can in source. Method handles and the Lookup API follow the same rule.

Java
public class Outer {
private void hello() { System.out.println("hello"); }
static class Inner {
void call(Outer o) throws Exception {
Outer.class.getDeclaredMethod("hello").invoke(o); // JDK 8: IllegalAccessException
} // JDK 11+: prints hello
}
}
System.out.println(Outer.Inner.class.getNestHost()); // class Outer
System.out.println(Arrays.toString(Outer.class.getNestMembers())); // [class Outer, class Outer$Inner]
System.out.println(Outer.Inner.class.isNestmateOf(Outer.class)); // true

The last three lines show the reflection API that JDK 11 added. getNestHost() returns the host, getNestMembers() returns the host followed by its members, and isNestmateOf() checks whether two classes share a host. Frameworks that generate code or check access, such as bytecode libraries and mocking tools, use these methods to follow the same rules the JVM does.

5Follow-up questions

Do lambdas and anonymous classes belong to the nest?

Anonymous and local classes compiled by javac are nest members of their top-level class. Lambdas are different: their bodies are compiled to private methods of the enclosing class, and the classes that implement them at runtime are hidden classes defined into the same nest, so they can call those private methods directly.

Does code compiled for Java 8 change behaviour when run on JDK 17?

No. Old class files have no nest attributes, so each class is a nest of one and the old access$ accessors are still in the bytecode and still work. The benefit appears only when the code is recompiled with a target of 11 or later.

Could a class join another nest to gain private access?

Not by itself. The JVM accepts a membership only when the host's NestMembers attribute lists the class and the classes are in the same package and loaded by the same class loader. Since the host lists its members at compile time, an outside class cannot add itself.

6Conclusion

In this answer, we saw that the Java language allowed private access between nested classes while the JVM did not, and that before JDK 11 javac bridged the gap with synthetic package-private accessors such as access$000, which grew the bytecode, weakened private and made reflection disagree with the language. JEP 181 in JDK 11 added nests through the NestHost and NestMembers attributes, so the JVM allows private access within a nest directly, the accessors disappeared, and Class.getNestHost(), getNestMembers() and isNestmateOf() expose nests to reflection.

What to say in the interviewAbout 80 seconds aloud

Open with

In source, private means private to the top-level class, including its nested classes, but to the JVM before JDK 11, Outer and Outer$Inner were just two separate classes in the same package, and private members were only accessible within their own class file.

Then, if they want more

So javac bridged the gap: when Inner read Outer's private field, javac generated a static synthetic method on Outer, named like access$000, with package access, that returned the field, and Inner called that.

It worked, but it added methods to every class file, it weakened private because any class in the package could call the accessor, and reflection followed JVM rules, so reflective private access between nested classes failed with IllegalAccessException even though the source compiled. JEP 181 in JDK 11 added nests.

The top-level class is the nest host and lists its members in a NestMembers attribute, and each nested class has a NestHost attribute pointing back. The JVM allows private access between classes with the same validated host, so javac now emits direct field access and method calls, the synthetic accessors are gone, and reflection matches the language.

At runtime, Class.getNestHost, getNestMembers and isNestmateOf expose the nest. Old class files keep their accessors until they are recompiled.

About this answer

Last updated
12 November 2026
Versions it names
Java 11, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do nested classes access each other's private members at the bytecode level, and...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/9609f7708a/