cannot be referenced from a static context

2018-01-17 10:23:31

报错:cannot be referenced from a static context,报错代码如下

public class neicunxielou {
    public static void main(String[] args)  throws Exception {


        MyThread myThread1 = new MyThread();
        myThread1.run();
    }

    private  class MyThread extends Thread{
        public void run(){
            System.out.println("hello");
        }
    }

}

原因如下

MyThread是一个非静态的内部类,只能被这个类的非静态方法访问。main方法是静态方法,使用该类创建对象时会出错。解决办法有两个:
1. 将MyThread变为静态的内部类,即加上static;
2. 将类移到外面定义。


修改代码如下


neicunxielou {
    main(String[] args)  Exception {


        MyThread myThread1 = MyThread();
        myThread1.run();
    }

    MyThread Thread{
        run(){
            System..println();
        }
    }

}


  • 2017-12-25 11:07:58

    mogodb 备份

    error connecting to db server: server returned error on SASL authentication

  • 2017-12-25 15:53:15

    MongoDB3.4版本配置详解

    配置文件方面,mongod和mongos有很多相同之处,下文中如有区别之处将会特别指出。 在一个节点上,通常同时启动mongod和mongos进程是正常的,他们之间不存在资源竞争,但是为了避免冲突,我们希望它们使用各自的配置文件,比如mongod.conf、mongos.conf;mongodb的某些平台下的安装包中没有自带配置文件,需要开发者自己创建。