I'm struggling on the concept of static vs. non-static. I'm studying this Thread example program(modified, from here originally), and when I use inner classes I will get the error:
Error: non-static variable this cannot be referenced from a static context
Here is the error-causing code:
public class Main2 {
public static void main(String[] args) {
Thread simple = new Thread(new SimpleTask());
simple.start();
}
class SimpleTask implements Runnable {
public void run() {
}
;
}
class DaemonTask implements Runnable {
public void run() {
int i = 0;
}
}
}
However, when I break it out so that it's 3 classes, there's no error.. ie if I make it:
public class Main2 {
/* contents */
}
class SimpleTask implements Runnable {
/* contents */
}
class DaemonTask implements Runnable {
/* contents */
}
Then it compiles just fine. Why does it matter that we split it out into another class?