This question already has an answer here:
- Closure in Java 7 [closed] 7 answers
I was reading Object Oriented Javascript and found the concept of closures. I didn't quite understand why and when it is used. Do other languages like Java also have closures? I basically want to understand how knowing the concept of closures can help me improve my coding.
A closure is a scoping technique. Java does not have closures.
In javascript you can do something like that following:
if you then do something like pass f to a function, scope is the scope of where it was defined.
Closures are know by various names in various languages but the essential points are as follows:
To create closures you need a language where the function type is a 1st class citizen i.e. it can be bound to a variable and passed around like any old string, int or bool.
You also need to be able to declare functions inline. In javascript you can do something like this:
To pass a anonymous function as a parameter to the foo function. We can use this to create a closure.
Closures "close over" variables so can be used to pass scoped variables around. Consider this example:
The side of eggs now contains a function which appends " and eggs" to whatever foodstuff it is passed. The spam variable is part of the foo function scope and would have been lost when the function exited, except that the closure "closed over" the namespace preserving it as long as the closure remains in memory.
So we're clear on closures having access to their parent's private scoped variables right? So how about using them to simulate private access modifiers in javascript?
So what's happening here is we're creating and immediately calling an anonymous function. There is one private variable in the function. It returns an object with a single method which references this variable. Once the function has exited the getConstant method is the sole way of accessing the variable. Even if this method is removed or replaced it will not give up it's secret. We have used closures to achieve encapsulation and variable hiding. For a more thorough explanation of this see http://javascript.crockford.com/private.html
Java does not have closures (yet). The closest it has are anonymous inner classes. However to instantiate one of these inline you have to instantiate an entire object (usually from an existing interface).The beauty of closures is they encapsulate simple, expressive statements which is somewhat lost in the noise of anonymous inner classes.
While Java doesn't have first-class functions, it does in fact have lexical closures.
For instance, the following Lisp function (stolen from Paul Graham's book On Lisp) returns a function that adds a number:
This can be done in Java. However, since it doesn't have first-class functions, we need to define an interface (let's call it Adder) and an anonymous inner class with a function that implements this interface.
The inner add() function is a lexical closure because it uses the n variable from the outer lexical scope.
The variable had to be declared final in order to do this, which means that the variable cannot change. However, changing values within reference variables is possible, even if they are final. For instance, consider the following Lisp function (also from On Lisp):
This can be implemented in Java by wrapping the outer variable in a reference type variable, such as an array or object.
I will claim that this is real lexical closures, not a simulation. But I won't claim that it's pretty.
Closure is a very natural feature that allows free-variables to be captured by their lexical environment.
here's an example in javascript:
function x returns a function. note that when a function is created variables used in this function are not evaluated like when we return an expression. when the function is created it looks to see what variables are not local to the function (free). It then locates these free variables and ensures they are not garbage collected so that they can be used once the function is actually called.
In order to support this feature you need to have first-class functions which java does not support.
Note this is a way we can have private variables in languages like JavaScript.
A closure is a first class function with bound variables.
Roughly that means that:
Java initially didn't have syntactic support for closures (these were introduced in Java 8), although it was fairly common practice to simulate them using anonymous inner classes. Here's an example: