java 无法对非静态类型 MyRunnable 进行静态引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12452864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:57:11  来源:igfitidea点击:

Cannot make a static reference to the non-static type MyRunnable

javastatic-methods

提问by kaiwii ho

Here is the whole code :

这是整个代码:

import java.util.ArrayList;

    public class Test<E extends Comparable<E>>{

    ThreadLocal<ArrayList<E>>arraylist=new ThreadLocal<ArrayList<E>>(){
        @Override
        protected ArrayList<E> initialValue() {
            // TODO Auto-generated method stub
            //return super.initialValue();
            ArrayList<E>arraylist=new ArrayList<E>();
            for(int i=0;i<=20;i++)
            arraylist.add((E) new Integer(i));
            return arraylist;
        }
    };


    class MyRunnable implements Runnable{

        private Test mytest;

        public MyRunnable(Test test){
            mytest=test;
            // TODO Auto-generated constructor stub
        }
        @Override
        public void run() {
                System.out.println("before"+mytest.arraylist.toString());
                ArrayList<E>myarraylist=(ArrayList<E>) mytest.arraylist.get();
                myarraylist.add((E) new Double(Math.random()));
                mytest.arraylist.set(myarraylist);
                System.out.println("after"+mytest.arraylist.toString());
            }

            // TODO Auto-generated method stub

        }
    public static void main(String[] args){

        Test test=new Test<Double>();

        System.out.println(test.arraylist.toString());

        new Thread(new MyRunnable(test)).start();

        new Thread(new MyRunnable(test)).start();

        System.out.println(arraylist.toString());

    }

}

my questions are:

我的问题是:

  1. Why new Thread(new MyRunnable(test)).start();cause the error:
    Cannot make a static reference to the non-static type MyRunnable?
  2. What does the term "static reference" refer to?
  1. 为什么new Thread(new MyRunnable(test)).start();会导致错误:
    Cannot make a static reference to the non-static type MyRunnable
  2. 术语“静态引用”指的是什么?

采纳答案by Amit Deshpande

Question 1: Why new Thread(new MyRunnable(test)).start(); cause the error: Cannot make a static reference to the non-static type MyRunnable?

Answer 1: Because you can not instantiate inner class in static context directly.mainmethod is always static. To Avoid this error you can use outer class reference to initialize inner class which binds that inner class to specified reference.

问题 1:为什么 new Thread(new MyRunnable(test)).start(); 导致错误:无法对非静态类型 MyRunnable 进行静态引用?

答案1:因为你不能直接在静态上下文中实例化内部类。main方法总是static。为避免此错误,您可以使用外部类引用来初始化将内部类绑定到指定引用的内部类。

 new Thread(test.new MyRunnable(test)).start();//Use test object to create new

Question 2: What does the term "static reference" refer to?

Answer 2: new MyRunnable(test)is not static you will have to make MyRunnablestatic in order to access like this.

问题2:术语“静态引用”指的是什么?

答案 2:new MyRunnable(test)不是静态的,您必须将其设为MyRunnable静态才能像这样访问。

You are using generics in most inefficient way :)

您正在以最低效的方式使用泛型:)

Declare MyRunnable<E>Because Generics in static context is different than in normal object context

声明MyRunnable<E>因为Generics in static context is different than in normal object context

If I understand you correctly you want MyRunnableclass to understand Ethat is passed to Testclass. This is not possible like you are doing.

如果我理解正确,您希望MyRunnable课堂能够理解E传递给Test课堂的内容。这是不可能的,就像你在做的那样。

You will have to make MyRunnableto understand Eand then you can access it.

您必须先MyRunnable理解E,然后才能访问它。

static class MyRunnable<E extends Comparable<E>> implements Runnable {

    private Test<E> mytest;

    public MyRunnable(Test<E> test) {
        mytest = test;
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run() {
        Test<Double> test = new Test<Double>();
        ArrayList<Double> doubles = test.arraylist.get();
        doubles.add(new Double(Math.random()));//No type cast needed
    }
    // TODO Auto-generated method stub

 }

回答by jtahlborn

you declared the MyRunnable class inside your Test class without the statickeyword, thus it is an "inner" class. you can only instantiate an inner class within an instance of an outer class. you are trying to instantiate it in a static method, hence there is no outer instance. my guess is that your intent is for the MyRunnable class to be a nested class not an inner class, so you should just add the static keyword to the class definition.

您在没有static关键字的情况下在 Test 类中声明了 MyRunnable 类,因此它是一个“内部”类。您只能在外部类的实例中实例化内部类。您试图在静态方法中实例化它,因此没有外部实例。我的猜测是您的意图是让 MyRunnable 类成为嵌套类而不是内部类,因此您应该将 static 关键字添加到类定义中。