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();
        }
    }

}


  • 2018-01-20 20:33:58

    PHP中session_cache_limiter() 是什么意思啊

    session_cache_limiter('public'); session_cache_expire(30); session_start();第三个我晓得 前两个 网络上没找着资料 手册上全是英文的 翻译后 还是看不懂主要是没翻译正确

  • 2018-01-23 02:22:17

    andorid,把控件置于最顶端。bringToFront的意外发现

     bringToFront方法在SDK中的说明是“Change the view's z order in the tree, so it's on top of other sibling views”,翻译过来就是改变view的z轴,使其处在他父view的顶端。关于bringToFront的实现,网上有很多资料介绍,大体来说就是将这个view从父view中移除,然后再加入父view的顶端。具体实现如何呢?