0

I haven't worked with closures in java 7 as yet and was wondering how they work and what is their main advantage or best use case in utilizing them?

Update:

I should have done my homework better. Here is the Project Lambda site for JSR 335 : Lambda Expressions for the Java Programming Language. They are claiming clousures will be in Java 8. I'll have to look into it more to see if that is really the case.

James Drinkard
  • 15,342
  • 16
  • 114
  • 137
  • 2
    They don't really work; you can't write to them. – SLaks Oct 24 '13 at 22:08
  • My mistake then. I thought they were fully in java 7 or would be for sure in java 8. – James Drinkard Oct 24 '13 at 22:41
  • I should have done my homework better. Here is the site: http://openjdk.java.net/projects/lambda/ They are claiming clousures will be in Java 8. I'll have to look into it more to see if that is really the case. – James Drinkard Oct 25 '13 at 13:54

1 Answers1

2

Java 7 has no closures. They've been rumored for a long time, and they are apparently set to appear in Java 8. Of course, I have been promised a Ghostbusters reboot that has been rumored too.

However, you can fake closures with anonymous inner classes. But make no mistake, these aren't closures.

As for the benefits of closures, I can't put it any better than Stack Overflow legend @jaif from this post:

"You can see it as a generalization of a class.

Your class holds some state. It has some member variables that its methods can use.

A closure is simply a more convenient way to give a function access to local state.

Rather than having to create a class which knows about the local variable you want the function to use, you can simply define the function on the spot, and it can implicitly access every variable that is currently visible.

When you define a member method in a traditional OOP language, its closure is "all the members visible in this class".

Languages with "proper" closure support simply generalize this, so a function's closure is "all the variables visible here". If "here" is a class, then you have a traditional class method.

If "here" is inside another function, then you have what functional programmers think of as a closure. Your function can now access anything that was visible in the parent function.

So it's just a generalization, removing the silly restriction that "functions can only be defined inside classes", but keeping the idea that "functions can see whatever variables are visible at the point where they're declared". "

Community
  • 1
  • 1
Vidya
  • 29,932
  • 7
  • 42
  • 70
  • They are no more closures than in java 8, which just introduces syntactic sugar for them. Or are they? – siledh Oct 24 '13 at 22:24
  • 1
    @siledh See http://programmers.stackexchange.com/q/195081/3907. Short answer: they are "mostly syntactic sugar". – Jon Onstott Oct 24 '13 at 22:31
  • Pre-Java-8, some closure capabilities can be faked using some features of the Google Guava library (https://code.google.com/p/guava-libraries/wiki/FunctionalExplained). That can make code pretty messy though; in Java 8 it'll be much better. – Jon Onstott Oct 24 '13 at 22:32
  • I think that's more faking first-class functions rather than faking closures, but that kind of discussion is academic splitting hairs and very unlikely to get me a date. – Vidya Oct 24 '13 at 22:37