没有封闭实例的分配引发了异常

时间:2016-07-05 10:26:06

标签: java

我收到此错误:

线程“main”中的异常java.lang.Error:未解决的编译问题:

No enclosing instance of type PrimitiveCasting is accessible. Must qualify the allocation with an enclosing instance of type PrimitiveCasting (e.g. x.new A() where x is an instance of PrimitiveCasting).

No enclosing instance of type PrimitiveCasting is accessible. Must qualify the allocation with an enclosing instance of type PrimitiveCasting (e.g. x.new A() where x is an instance of PrimitiveCasting).


at casting.PrimitiveCasting.main(PrimitiveCasting.java:22)

包裹铸造;

public class PrimitiveCasting {
    class anand {
        int a = 90;

        void anand1() {
            System.out.println("anand is having anand1");
        }
    }

    class babu extends anand {
        int c, b = 88;

        void babu1() {
            System.out.println("babu is having babu1");
        }
    }

    public static void main(String[] args) {
        System.out.println("**********");
         anand z1= new anand();
         z1.anand1();

         babu b1= new babu();
         b1.anand1();
         b1.babu1();`enter code here`
         System.out.println("********");
    }

}

4 个答案:

答案 0 :(得分:1)

这是正常的,在您的代码中,您将anand类定义为与PrimitiveClass的实例绑定,即,您可以从PrimitiveClass的实例中获取anand类的实例。这就是我们所谓的内部阶级。 为了使您的代码能够工作,您可以将anand(和babu1)类标记为静态。 这是代码:

public class PrimitiveCasting {
static class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

static class babu extends anand {
    int c, b = 88;

    void babu1() {
        System.out.println("babu is having babu1");
    }
}

public static void main(String[] args) {
    System.out.println("**********");
     anand z1= new anand();
     z1.anand1();

     babu b1= new babu();
     b1.anand1();
     b1.babu1();
     System.out.println("********");
}

}

或者您可以创建一个PrimitiveClass实例,然后实例化anand或babu,如下所示:

public class PrimitiveCasting {
class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

class babu extends anand {
    int c, b = 88;

    void babu1() {
        System.out.println("babu is having babu1");
    }
}

public static void main(String[] args) {
    System.out.println("**********");
    PrimitiveCasting primitiveCasting = new PrimitiveCasting();
    anand z1= primitiveCasting.new anand();
    z1.anand1();

    babu b1= primitiveCasting.new babu();
    b1.anand1();
    b1.babu1();
    System.out.println("********");
}

}

希望这有帮助!

答案 1 :(得分:0)

你应该以这种方式创建an的实例。

PrimitiveCasting pc = new PrimitiveCasting();
anand z1 = pc.new anand();

因为你从静态上下文中引用anand,所以应该使用封闭的实例类型进行限定。

其他类也是如此。

答案 2 :(得分:0)

由于内部类不是静态成员变量,因此无法从静态方法main访问。要么将这些内部类静态化为: -

public class PrimitiveCasting  {
static class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

或者在main方法中创建PrimitiveCasting实例以访问这些类: -

PrimitiveCasting pc = new PrimitiveCasting();

     anand z1= pc.new anand();
     z1.anand1();

答案 3 :(得分:0)

Java编程语言允许您在另一个类中定义类。这样的类称为嵌套类 声明为static的嵌套类称为静态嵌套类。非静态嵌套类称为内部类 要实例化内部类,必须首先实例化外部类。然后,在外部对象中创建内部对象:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html