No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

Posted 战斗的小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing相关的知识,希望对你有一定的参考价值。

package com.thread;

public class Thread01 {
    public class Thread1 extends Thread{
        private String name;
        public Thread1(String name){
            this.name = name;
        } 
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println(name+"运行:"+i);
                try {
                    sleep((int) Math.random()*10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    public static void main(String[] args){
        Thread1 thread02 = new Thread1("A");
        Thread1 thread02 = new Thread1("B");
        
    }
}

在写这段代码的时候,编译器在Thread1 thread02 = new Thread1("A");一直会报No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing的错误,我在网上查了资料发现:在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。

我们可以将将 public class Thread1 改为public static class Thread1

 

以上是关于No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing的主要内容,如果未能解决你的问题,请参考以下文章

No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

Java中出现No enclosing instance of type XXX is accessible问题

No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

Java变异出现错误:No enclosing instance of type XXX is accessible

Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an encl

java出现No enclosing instance of type main is accessible. Must qualify the allocation with an enclosin