Why java doesn't allow to create instance of inner class?

This question already has an answer here:

  • Java inner class and static nested class 23 answers

  • Non static Inner classes are treated as members of outer class.
  • To create their instances, you need to use reference of outer class.
  • So you have to do something like this,

    OuterClass outer = new OuterClass();
    InnerClass inner = outer.new InnerClass();
    

    So, in your case,

    m obj = new m();
    sub1 s1 = obj.new Sub1();
    
    链接地址: http://www.djcxy.com/p/92018.html

    上一篇: Java访问静态嵌套类

    下一篇: 为什么java不允许创建内部类的实例?