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.
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.
public class MyCallbackImpl implements Callback
. –
Lurcher doWork()
method. –
Lurcher 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);
By using the callback mechanism we will get our own method implementation calling back only. or a particular implementation when the event has been clicked. It will be used mostly in EventHandlers in java.
package com.callbackExample;
public abstract interface SomeEventHandler {
public abstract void hadleClick();//by default abstract
}
package com.callbackExample;
public class SomeEventImplementation implements SomeEventHandler {
@Override
public void hadleClick() {
System.out.println("Click Handler : clicked");
}
}
package com.callbackExample;
public class Button {
public void onClick(SomeEventHandler clickEventHandler) {
clickEventHandler.hadleClick();
}
}
package com.callbackExample;
public class Test {
public static void main(String[] args) {
Button button=new Button();
SomeEventImplementation someEventImplementation=new SomeEventImplementation();
button.onClick(someEventImplementation);
Button button2=new Button();
button2.onClick(new SomeEventHandler() {
@Override
public void hadleClick() {
System.out.println("button2 : my own implementation..");
}
});
}
}
-------------------------------------------
OUTPUT : Click Handler : clicked
button2 : my own implementation..
-------------------------------------------
In simple terms, callback mechanism refers to calling a function with another function as an argument. In languages like C,C++ this is done by passing function pointers as arguments but java doesn't have the concept of pointers. The workaround is interfaces. We pass reference to interfaces instead of pointers. Your understanding will be crystal clear after understanding the code below. To also show the real world applications, imagine purchasing a mouse and a mouse pad. The mouse pad price is fixed but mouse price differs by brand.
interface mouse
{
double mousePrice();
}
class BrandA implements mouse
{
public double mousePrice() //note that public access modifier is needed as all methods of interface are public are by default and when you override them
//you cannot use any access modifier more restrictive
{
return 100;
}
}
class BrandB implements mouse
{
public double mousePrice()
{
return 200;
}
}
class Simple
{
static void total(mouse t)
{
double mousepad = 20;
double mousep = t.mousePrice();
System.out.println(mousepad + mousep);
}
public static void main(String args[])
{
mouse ob = new BrandA(); //upcasting.
total(ob);
}
}
@Sotirios Delimanolis answer is good but I wanted to give clear example which explains callbacks in a way how listeners works - following approach is greatly adopted by android library.
class RemoteClass {
private OnChangeListener mOnChangeListener;
void makeSomeChanges() {
/*
.. do something here and call callback
*/
mOnChangeListener.onChanged(this, 1);
}
public void setOnChangeListener(OnChangeListener listener) {
mOnChangeListener = listener;
}
public interface OnChangeListener {
public void onChanged(RemoteClass remoteClass, int test);
}
}
There is a class built my someone, which goes by name RemoteClass
and tells your class to reference the callback by passing implementation of OnChangeListener
interface to setOnChangeListener
method.
class Test {
public static void main(String[] args) {
RemoteClass obj = new RemoteClass();
obj.setOnChangeListener(demoChanged);
obj.makeSomeChanges();
}
private static RemoteClass.OnChangeListener demoChanged = new RemoteClass.OnChangeListener() {
@Override
public void onChanged(RemoteClass remoteClass, int incoming) {
switch (incoming) {
case 1:
System.out.println("I will take appropriate action!");
break;
default:
break;
}
}
};
}
Now your class has finished doing its task and RemoteClass
does its work and upon calling makeSomeChanges
whenever necessary results in onChanged
method execution using mOnChangeListener
reference.
Calling the onSuccess or onFailure callback methods, based on the status doing work.
package mains.geeks;
public class Test {
public static void main(String[] args) throws Exception {
Test testObj = new Test();
testObj.doWork(new Callback() {
@Override
public void onSucess() {
System.out.println("success callback called");
}
@Override
public void onFailure() {
System.out.println("onFailure callback called");
}
});
}
public void doWork(Callback callback) {
System.out.println("doing work");
if (false){
callback.onSucess();
} else {
callback.onFailure();
}
}
public interface Callback {
void onSucess();
void onFailure();
}
}
© 2022 - 2024 — McMap. All rights reserved.