Cannot make a static reference to the non-static type MyRunnable
Posted
by
kaiwii ho
on Stack Overflow
See other posts from Stack Overflow
or by kaiwii ho
Published on 2012-09-17T03:34:54Z
Indexed on
2012/09/17
3:37 UTC
Read the original article
Hit count: 189
Here is the whole code : import java.util.ArrayList;
public class Test>{
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 the new Thread(new MyRunnable(test)).start();
cause the error:
Cannot make a static reference to the non-static type MyRunnable
?
2\
what is the static reference refer to right here? thx in advanced
© Stack Overflow or respective owner