为内部 class 创建对象时出错(嵌套 class 中的 class)

Error while creating object for inner class (class within nested class)

这是我正在尝试的代码;我在行“outer.inner1.inner2 innerobj2= outerobj.new inner2();”上收到错误。

谁能确认为嵌套 class 方法创建对象的错误和语法是什么?

class outer {

  public void outerDisplay() { 
    System.out.println("Statement from outer class");
  }

  class inner {
    public void innerDisplay() {
        System.out.println("Statement from inner class");   
    }
  }

  class inner1 {
    public void inner1Display() {
        System.out.println("Statement from inner1 class");
    }

    class inner2 {
        public void inner2Display() {
            System.out.println("Statement from inner2 class");  
        }
    }
  }
}


public class InnerOuter {

  public static void main(String[] args) {
    outer outerobj=new outer();
    outerobj.outerDisplay();

    outer.inner innerobj=outerobj.new inner();
    innerobj.innerDisplay();

    outer.inner1 innerobj1=outerobj.new inner1();
    innerobj1.inner1Display();

    **outer.inner1.inner2 innerobj2= outerobj.new inner2()**;
    innerobj2.inner2Display();      
  }
}

Class inner2inner1 的内部 class 而不是 outer。 因此,要创建 inner2 的实例,您需要 inner1 的实例,如下所示:

outer.inner1.inner2 innerobj2= innerobj1.new inner2();

outer.inner1.inner2 innerobj2= outerobj.new inner1().new inner2();