1. What is a callback function
Callback function is a function called by function pointer. If you pass a function pointer (address) as a parameter to another function, when the pointer is used to call the function it points to, we say it is a callback function. In Java, pointers are called references. </ u> the callback function is not called directly by the implementer of the function, but by another party when a specific event or condition occurs, which is used to respond to the event or condition.
A callback method is any method that is called by another method with the callback method as its first parameter. Many times, a callback is a method that is called when something happens.
2. Application scenarios
Event driven mechanism
For example: Party A hires Party B to repair the car; then Party A goes to do other things; Party B notifies Party A that the car has been repaired, please come and get it. There is no need for Party A to wait for Party B to finish repairing the car.
3. Code examples
1. Test class
package com.callBack;
public class CallBckTest {
public static void main(String[] args) {
MainBusiness mainBusiness = new MainBusiness();
System.out.println("*********The specific implementation class implements the callback method *********");
mainBusiness.execute(new CallbackServiceImpl());
System.out.println("********* anonymous internal class implements the callback method ********* flexible *********");
mainBusiness.execute(new CallbackService() {
public void callBackFunc() {
System.out.println("The anonymous internal class callback function started executing...") ;
System.out.println("Anonymous internal class callback function ends execution...\n");
}
});
}
}
2. Business class and method
package com.callBack;
public class MainBusiness {
private CallbackService callback;
public void execute(CallbackService callback) {
this.callback = callback;
callBack();
}
public void callBack() {
callback.callBackFunc();
}
}
3. Callback function interface
package com.callBack;
//Callback functions of the interface and methods
public interface CallbackService {
void callBackFunc();
}
4. Callback function implementation class
package com.callBack;
public class CallbackServiceImpl implements CallbackService {
@Override
public void callBackFunc() {
System.out.println("The concrete implementation class callback function starts execution...") ;
System.out.println("Concrete implementation class callback function ends execution...\n");
}
}
5. Print results
*********Concrete implementation class implements the callback method *********
Concrete implementation class callback function starts execution...
Concrete implementation class callback function ends execution...
********* callback methods implemented by anonymous internal classes *********
Anonymous internal class callback function start execution...
Anonymous internal class callback function ends execution...
Read More:
- Asynchronous callback case of Java callback function
- Implementation of retrial mechanism in Java
- The thread implementation of timer in Java
- Java implementation of inputsteam to Base64
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- Problems and causes of Java’s main function format (public static void main (string args()))
- Code case of XXL job executor
- [Solved] port (127.0.0.1:64444): java.net.SocketException “Interrupted function call: accept failed“
- C++ String case conversion and transform, tower, upper, usage
- C++: Implementation of multi-channel IO transfer with select
- mybatis “case when” Error: Failed to process, please exclude the tableName or statementId.
- [Solved] In case of “transactionmanager” while setting bean property “transactionmanager” error
- C language: Implementation of dynamic array initialization, insertion, traversal, deletion and destruction
- [Solved] The Bean Validation API is on the classpath but no implementation could be found
- [Solved] Jxls error: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- Java error: java.lang.NoSuchMethodError
- Abstract method and static method of java interface
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException