Java (and other languages) supports nested classes through inner classes. To make this work, the compiler needs to perform some tricks. Here’s an example:

public class Outer {
    private int outerInt;

     class Inner {
       public void printOuterInt(a) {
         System.out.println("Outer int = "+ outerInt); }}}Copy the code

Before compiling, the compiler modifies it to create something like this:

public class Outer {
  private int outerInt;

  public int access$000() {
    returnouterInt; }}class Inner$Outer {

  Outer outer;

  public void printOuterInt(a) {
    System.out.println("Outer int = " + outer.access$000()); }}Copy the code

Although the inner class is logically part of the same code entity as the outer class, it is compiled as a separate class. Therefore, it requires the compiler to create synthetic bridge methods to provide access to the private fields of the external class.

This JEP introduces the concept of a nest, in which two members of the same nest (outer and inner in our example) are cohomed. Two new attributes are defined for the class file formats NestHost and NestMembers. These changes are useful for other languages that support nested classes and compile to bytecode.

This feature introduces three new methods to java.lang.class:

  • Class getNestHost()
  • Class[] getNestMembers()
  • boolean isNestmateOf(Class)

This feature also requires changes to the Java Virtual Machine Specification (JVMS), specifically section 5.4.4, “Access Control.”