Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call backs in Java (code explanation)

Tags:

java

callback

I came across this question about callbacks in Java. Hers is the running code and original answer here.

  1. But I didn't understand how is it useful for callback?
  2. Can you explain the concept of callback to a Java programmer?

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));

    }
}
like image 547
zengr Avatar asked Oct 28 '10 09:10

zengr


People also ask

What are call backs in Java?

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).

What is a call back coding?

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.

What is callback explain with an example?

"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.


2 Answers

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 :

  1. The main() function actually is a number (read Data) stream parsing algorithm, which will parse a pair of numbers (data sets) and then operate on them.
  2. Now, considering separation of concerns, the main() function should neither know or understand what operations can be performed on the datasets. In fact it should not even care of the data types of the data sets.
  3. However, since these data sets are specific to your domain, the main function should ideally delegate the processing of the data to your domain specific classes.
  4. For it to do so, it must enforce a contract with the domain specific caller (the caller for main) and say - look, I'll call the 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.

like image 85
madhurtanwani Avatar answered Sep 28 '22 16:09

madhurtanwani


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.

like image 40
Denilson Sá Maia Avatar answered Sep 28 '22 15:09

Denilson Sá Maia