如何实例化非静态内部类?

时间:2019-12-13 17:48:59

标签: java inner-classes non-static

我有一个内部类,我想从父类的静态方法中实例化如下:

public class MyClass {
    public class B {
        private int v;
        B(int x) {
            v = x;
        }

        public void pr() {
            System.out.println(this.v);
        }
    }

    public static void main(String args[]) {
      int x=10;
      int y=25;
      int z=x+y;
    B bb = new B(3);
      System.out.println("Sum of x+y = " + z);
    }
}

它抛出如下错误:

error: non-static variable this cannot be referenced from a static context
    B bb = new B(3);
           ^
1 error

0 个答案:

没有答案
相关问题