Prev Next

Java / Control Statements

Advantages of declaring loop variable as final in enhanced for-loop.

  • Enables using the loop variable in an anonymous inner class within the loop body.
for (final int i : intList) {
	Runnable run = new Runnable() {
		public void run() {
			process(i);
		}
	};
	new Thread(run).start();
}

  • It prevents the loop variable to be accidently changed inside the loop while iterating.

More Related questions...

Show more question and Answers...


Comments & Discussions