105

What is closure? It is supposed to be included in Java 7. (Closures were discussed for inclusion in Java 7, but in the end were not included. -ed) Can anyone please provide me with some reliable references from where I can learn stuff about closures?

Cheeso
  • 180,104
  • 92
  • 446
  • 681
Swaranga Sarma
  • 12,215
  • 19
  • 54
  • 87

7 Answers7

84

A closure is a block of code that can be referenced (and passed around) with access to the variables of the enclosing scope.

Since Java 1.1, anonymous inner class have provided this facility in a highly verbose manner. They also have a restriction of only being able to use final (and definitely assigned) local variables. (Note, even non-final local variables are in scope, but cannot be used.)

Java SE 8 is intended to have a more concise version of this for single-method interfaces*, called "lambdas". Lambdas have much the same restrictions as anonymous inner classes, although some details vary randomly.

Lambdas are being developed under Project Lambda and JSR 335.

*Originally the design was more flexible allowing Single Abstract Methods (SAM) types. Unfortunately the new design is less flexible, but does attempt to justify allowing implementation within interfaces.

Tom Hawtin - tackline
  • 139,906
  • 30
  • 206
  • 293
60

Here is Neal Gafter's blog one of the pioneers introducing closures in Java. His post on closures from January 28, 2007 is named A Definition of Closures On his blog there is lots of information to get you started as well as videos. An here is an excellent Google talk - Advanced Topics In Programming Languages - Closures For Java with Neal Gafter, as well.

mkobit
  • 34,772
  • 9
  • 135
  • 134
Bartzilla
  • 2,740
  • 3
  • 23
  • 36
7

According to Tom Hawtin

A closure is a block of code that can be referenced (and passed around) with access to the variables of the enclosing scope.

Now I'm trying to emulate the JavaScript closure example on Wikipedia, with a "straigth" translation to Java, in the hope to be useful:

//ECMAScript
var f, g;
function foo() {
  var x = 0;
  f = function() { return ++x; };
  g = function() { return --x; };
  x = 1;
  print('inside foo, call to f(): ' + f()); // "2"  
}
foo();
print('call to g(): ' + g()); // "1"
print('call to f(): ' + f()); // "2"

Now the java part: Function1 is "Functor" interface with arity 1 (one argument). Closure is the class implementing the Function1, a concrete Functor that acts as function (int -> int). In the main() method I just instantiate foo as a Closure object, replicating the calls from the JavaScript example. The IntBox class is just a simple container, it behave like an array of 1 int:

int a[1] = {0}
interface Function1   {
    public final IntBag value = new IntBag();
    public int apply();
}

class Closure implements Function1 {
   private IntBag x = value;
   Function1 f;
   Function1 g;

   @Override
   public int apply()  {
    // print('inside foo, call to f(): ' + f()); // "2"
    // inside apply, call to f.apply()
       System.out.println("inside foo, call to f.apply(): " + f.apply());
       return 0;
   }

   public Closure() {
       f = new Function1() {
           @Override
           public int apply()  {
               x.add(1);
                return x.get();
           }
       };
       g = new Function1() {
           @Override
           public int apply()  {
               x.add(-1);
               return x.get();
           }
       };
    // x = 1;
       x.set(1);
   }
}
public class ClosureTest {
    public static void main(String[] args) {
        // foo()
        Closure foo = new Closure();
        foo.apply();
        // print('call to g(): ' + g()); // "1"
        System.out.println("call to foo.g.apply(): " + foo.g.apply());
        // print('call to f(): ' + f()); // "2"
        System.out.println("call to foo.f.apply(): " + foo.f.apply());

    }
}

It prints:

inside foo, call to f.apply(): 2
call to foo.g.apply(): 1
call to foo.f.apply(): 2 
Thomas Fritsch
  • 7,861
  • 33
  • 31
  • 41
Rob Rosetti
  • 71
  • 1
  • 3
6

Please see this wiki page for definition of closure.

And this page for closure in Java 8: http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

Also look at this Q&A: Closures in Java 7

Community
  • 1
  • 1
anubhava
  • 664,788
  • 59
  • 469
  • 547
4

Java Closures are going to be a part of J2SE 8 and is set to be released by the end of 2012.

Java 8's closures support include the concept of Lambda Expressions, Method Reference, Constructor Reference and the Default Methods.

For more information and working examples for this please visit: http://amitrp.blogspot.in/2012/08/at-first-sight-with-closures-in-java.html

gnat
  • 6,199
  • 101
  • 49
  • 71
3

Yes,Closure (Lambda Expressions) is the new feature with the upcoming Java SE 8 release. You can get more info about this from the below link: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Anurag Ratna
  • 301
  • 4
  • 11
0

A closure implementation for Java 5, 6, and 7

http://mseifed.blogspot.se/2012/09/bringing-closures-to-java-5-6-and-7.html

It contains all one could ask for...

mmm
  • 18,431
  • 26
  • 99
  • 165
  • The link goes to some text describing it but the code linked to from that page doesn't exist. Where is the implementation? – Jerry Jeremiah Oct 14 '14 at 23:06
  • @JerryJeremiah Here it is, it has different name now, also all of the versions orignally present are not there ( to support non-final access and more through abstract classes, it might be added again for < Java 8 versions, what platform are you looking for?) : https://bitbucket.org/momomo/opensource/src – mmm Oct 16 '14 at 10:55
  • 2
    Links are breaking! – sunleo Mar 04 '17 at 15:26
  • i got a 404 the link is dead – Alexander Mills Feb 15 '19 at 20:09
  • link broken (this is why we should not put links as answers...) – OhadR Jan 22 '20 at 09:38