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
Cannot make a static reference to the non-static type MyRunnable
提问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:
我的问题是:
- Why
new Thread(new MyRunnable(test)).start();
cause the error:Cannot make a static reference to the non-static type MyRunnable
? - What does the term "static reference" refer to?
- 为什么
new Thread(new MyRunnable(test)).start();
会导致错误:Cannot make a static reference to the non-static type MyRunnable
? - 术语“静态引用”指的是什么?
采纳答案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.
main
method is alwaysstatic
. 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 makeMyRunnable
static 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 MyRunnable
class to understand E
that is passed to Test
class.
This is not possible like you are doing.
如果我理解正确,您希望MyRunnable
课堂能够理解E
传递给Test
课堂的内容。这是不可能的,就像你在做的那样。
You will have to make MyRunnable
to understand E
and 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 static
keyword, 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 关键字添加到类定义中。