Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a callback method in Java? (Term seems to be used loosely)

I don't understand what a callback method is and I have heard people use that term very loosely. In the Java world, what is a callback method? If someone could provide some example code of a Java callback method with an explanation, it would be a great help in my Java learning journey.

like image 291
Horse Voice Avatar asked Oct 16 '13 13:10

Horse Voice


People also ask

What is callback method?

Android Callback Listeners Example:Android maintains the interaction between the end-user and the application using the widely used Listener Design Pattern. All the UI components, like a button, inherit from the View class, which in turns implements the Callback interface from android. graphics.

What are the types of callback?

There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks).

What is callback function and how it works?

A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.


2 Answers

A callback is a piece of code that you pass as an argument to some other code so that it executes it. Since Java doesn't yet support function pointers, they are implemented as Command objects. Something like

public class Test {
    public static void main(String[] args) throws  Exception {
        new Test().doWork(new Callback() { // implementing class            
            @Override
            public void call() {
                System.out.println("callback called");
            }
        });
    }

    public void doWork(Callback callback) {
        System.out.println("doing work");
        callback.call();
    }

    public interface Callback {
        void call();
    }
}

A callback will usually hold reference to some state to actually be useful.

By making the callback implementation have all the dependencies to your code, you gain indirection between your code and the code that is executing the callback.

like image 188
Sotirios Delimanolis Avatar answered Oct 22 '22 06:10

Sotirios Delimanolis


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

Also in bigger and more complex systems you simply can annotate a method and the system will identify all annotated methods and will call them when the event occurs (see example 2). Of course the system defines what parameters the method should receive and other constraints.

Example 1:

public interface Callback {
    //parameters can be of any types, depending on the event defined
    void callbackMethod(String aParameter);
}


public class CallbackImpl implements Callback {
    void callbackMethod(String aParameter) {
     //here you do your logic with the received paratemers
     //System.out.println("Parameter received: " + aParameter);

    }
}

//.... and then somewhere you have to tell the system to add the callback method
//e.g. systemInstance.addCallback(new CallbackImpl());

Example 2:

//by annotating a method with this annotation, the system will know which method it should call. 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CallbackAnnotation {}


public class AClass {

    @CallbackAnnotation
    void callbackMethod(String aParameter) {
     //here you do your logic with the received paratemers
     //System.out.println("Parameter received: " + aParameter);

    }
}

//.... and then somewhere you have to tell the system to add the callback class
//and the system will create an instance of the callback class
//e.g. systemInstance.addCallbackClass(AClass.class);
like image 12
V G Avatar answered Oct 22 '22 07:10

V G