Why do variables passed to runnable need to be final?
问题 If I have a variable int x = 1 , say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final . Why? final int x = 0;//<----must be final... private class myRun implements Runnable { @Override public void run() { x++;// } } 回答1: Because if they are able to be changed, it could cause a lot of problems, consider this: public void count() { int x; new Thread(new Runnable() { public void run() { while(x < 100) { x++; try { Thread