码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-06-28 18:47:31      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:mat   style   编译器   private   成员   静态类   sys   div   extends   

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

标签:mat   style   编译器   private   成员   静态类   sys   div   extends   

原文地址:http://www.cnblogs.com/L-a-u-r-a/p/7090509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!