I asked a question about currying and closures were mentioned. What is a closure? How does it relate to currying?
A closure may be a named or anonymous function, but is known as such when it "closes over" variables in the scope where the function is defined, i.e., the closure will still refer to the environment with any outer variables that are used in the closure itself.
Note though that a closure doesn't need to be a self-invoking function, but it can be. When a closure is self invoking (i.e. immediately called by adding () after the function), this means the return value is immediately calculated, rather than the function being returned and the return value being calculated later once the function is invoked.
When you create the closure, i is a reference to the variable defined in the outside scope, not a copy of it as it was when you created the closure. It will be evaluated at the time of execution. Most of the other answers provide ways to work around by creating another variable that won't change the value for you.
How to ensure closures created in a loop capture the value of the loop ...
A closure carries parts of a local state into a function of some sort, think of it as passing by reference. A callback is meant to notify you about certain change and it redirects program flow. The closure could modify local state but you would never get processor time to handle that, like you would with a callback.
A closure is a first class function with bound variables. Roughly that means that: You can pass the closure as a parameter to other functions The closure stores the value of some variables from the lexical scope that existed at the time that is was created Java initially didn't have syntactic support for closures (these were introduced in Java 8), although it was fairly common practice to ...