Why does the local variable of an enhanced for loop have to be local? [duplicate]
Look at how the for-each loop internally works, see How does the Java ‘for each’ loop work? for(Iterator<String> i = someList.iterator(); i.hasNext(); ) { String item = i.next(); System.out.println(item); } Each time it declares the String variable item. Hence in your case its doing essentially State state; \\and inside State state = i.next(); which obviously … Read more