I came across this question about callbacks in Java. Hers is the running code and original answer here.
Code:
public class Main {
public interface Visitor {
int DoJob(int a, int b);
}
public static void main(String[] args) {
Visitor adder = new Visitor(){
public int DoJob(int a, int b) {
return a + b;
}
};
Visitor multiplier = new Visitor(){
public int DoJob(int a, int b) {
return a*b;
}
};
System.out.println(adder.DoJob(10, 20));
System.out.println(multiplier.DoJob(10, 20));
}
}
A callback method in java is a method that gets called when an event (call it E ) occurs. Usually you can implement that by passing an implementation of a certain interface to the system that is responsible for triggering the event E (see example 1).
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.
"I will call back later!" A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.
I wrote a small blog post on this sometime back : http://madhurtanwani.blogspot.com/2010/09/callbacks-in-java.html. Hope it helps!
Before I try explaining the above code post, I must say, its not the most intuitive or good use of call backs. The example I've used in my post is of Collections.sort() which clearly brings out the callback part.
Neverthelss, for the code posted above, think of like this :
doJob
method on a Visitor
interface
implementations, whenever I receive a pair of data sets. What the caller must do is implement the Visitor interface and implement the domain specific logic to process the datasets.The part of delegating processing from the caller, back to the callee is called a callback implemented using interface
(contract specification) in Java.
I don't want to start a flame war here... But the concept of callbacks is a lot easier to understand in languages like C/C++, JavaScript, Python, probably Ruby and many others. In such languages, a callback is just a function pointer. You pass your function as this function pointer, and the other code will call back your function using that pointer. As simple as that. (look at this C example from Wikipedia)
But Java does not have function pointers, and thus the Java programmer is required to use Anonymous Inner Classes, Interfaces and things like that in order to encapsulate a function inside a class, and pass an instance of that class as the callback.
I think I managed to answer your second question ("Can you explain the concept of callback to a Java programmer?"), but please see the other answers about how to implement that in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With