Closures

Finally, what is a closure? With the previous example, lets assume the anonymous function wants to access variables from the enclosing class. In this case, the compiler needs to create a “closure” – that is, it needs to make sure that the variables are still available whenever the anonymous function is called, even after they are already out of scope. That is the reason why, for Java inner classes, only final variables from the enclosing class can be accessed from within the inner class (remember – a Java inner class is a kind of a closure).