tags:

views:

238

answers:

8

Is there any other way in java to implement call backs apart from inner classes? What is the difference between callbacks and closures?

A: 

I don't think so.

If there is, then it is probably inferior in some way, otherwise anonymous inner classes wouldn't be widely used.

Matt H
A: 

There is no difference.

Closures can be defined as a block of code holding parent context that can be executed with ease.

In fact, the only difference I know between those is the ease of writing. A typical groovy/ruby closure is indeed smaller to write than a Java anonymous class.

However, considering Java framworks like guava and there liberal use of anonymous classes/interfaces, particularly for typical closures use cases like filter (comparing with groovy's implementation), I can say there is absolutely no design difference.

Riduidel
If there is no difference then why these two names exist. I am clueless
Neeraj
A: 

Sadly the only reasonable way is inner/anonymous classes.

You can also do it with reflection, but that usually is slower and harder in maintenance (no syntax highlighting, hard to find references in IDE etc.). An example:

myButton.addActionListener(EventHandler.create(ActionListener.class, handlerObject, "onClick"));
Konrad Garus
+3  A: 

Closure is how you build it, callback is how you use it.

A callback can be implemented as a closure (in languages that have them) or an implementation of an interface (in Java, as an anonymous inner class or a regular class).

Callback means that you pass a piece of code to a function, so that the function can call that piece of code later. It is a special kind of parameter.

The piece of code can be a function pointer or a closure or an object with well-known methods, depending on what the language offers.

Thilo
This was the answer I was trying to write when Neeraj first dropped the question but couldn't get out in a coherent fashion. Kudos.
JUST MY correct OPINION
A: 

For now anonymous classes are the best way of handling callbacks in Java. However this is likely to change come Java 7 which will implement closures. http://en.wikipedia.org/wiki/Closure_(computer_science)

Mex
+1  A: 

A callback is just any executable code that is passed as a parameter to other code. In frequent usage, that executable code is a closure, but it's not necessarily.

The word closure is somewhat abused and many people just use it as a synonym for "anonymous function", but at least according to Wikipedia, that's a misuse of the term. The Wikipedia article explains this better than I can do quickly.

Don Roby
+1  A: 

Both closures and anonymous inner classes (and others) can be used as callbacks. A callback is just some code which is passed as an argument to other code.

A big difference of closures, compared to Java's anonymous inner classes, is that (in imperative languages) a closure can modify the variables of the surrounding scope. Wikipedia gives the following example:

var f, g;
function foo() {
  var x = 0;
  f = function() { return ++x; };
  g = function() { return --x; };
  x = 1;
  alert('inside foo, call to f(): ' + f()); // "2"
}
foo();
alert('call to g(): ' + g()); // "1"
alert('call to f(): ' + f()); // "2"
Esko Luontola
Although this can be emulated by `final ObjRef<Integer> x=new ObjRef<Integer>(0);` with `ObjRef` implemented as mutable reference.
Ha
Actually Scala does something like that to implement closures on the JVM, because JVM (and Java) does not support them natively.
Esko Luontola
A: 

If you need closures in java you could try lambdaj. Here you can see how it allows to define closures through a very straightforward DSL.

Mario Fusco
Actually, lambdaj does not support the creation of closures. At best it supports the creation of some kinds of lambda expressions that are not closures.
Neal Gafter