public class Thread8 implements Runnable {
Demo d;
public static void main(String r[]) {
new Thread8().disp(); }
void disp() {
d = new Demo();
new Thread(new Thread8()).start();
new Thread(new Thread8()).start();
}
public void run() { d.fun(Thread.currentThread().getId()); }
}
class Demo {
static long f=0;
synchronized void fun(long id) {
for(int i=1; i<3; i++){
System.out.println(id);
Thread.yield();
}
}
}
The program compiles properly but gives a NullPointerException. Why?
Why did it not give a compile error for using a non-static reference variable d [Demo d] in static context i.e. in void disp()?