http://d.hatena.ne.jp/nowokay/comment?date=20130522#c
import java.util.List; import java.util.function.Supplier; import java.util.ArrayList; public class MyClass { public static void main(String args[]) { List<Supplier<String>> list = new ArrayList<>(); for(int i = 1; i <= 10; ++i){ list.add( () -> String.format("%d番目", i)); } list.forEach(s -> System.out.println(s.get())); } }
Online Java Compiler - Online Java Editor - Java Code Online - Online Java Runnerで実行
/MyClass.java:9: error: local variables referenced from a lambda expression must be final or effectively final list.add( () -> String.format("%d番目", i)); ^ 1 error