No enclosing instance of type 类名 is accessible. Must qualify the allocation with an enclosing instan

    xiaoxiao2025-06-08  42

    下面是我写的代码,一个经典的内部类

    public class Value{ public static void main(String[] args) { Test y=new Test(); } class Test{ public Test() { System.out.println("yes"); } } }结果在编译阶段就报错了:

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

    没有可访问的内部类Value的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是Value的实例。)

    原来是因为我写的Value类是动态的,而public static void main方法是静态的,我们没办法在静态方法内去实例化一个动态对象 因此最简单的方法就是把class Test 换成 static class Test

    或者在别的动态方法内调用动态方法

    转载请注明原文地址: https://ju.6miu.com/read-1299715.html
    最新回复(0)