The methods which are called implicitly by the software are known as callback methods. That is, callback methods are called automatically. Programmer writes them but never call them. They are called by the software at an appropriate time when required for the smooth execution of the program. Let us explain further through examples.
Examples of Callback methods in Java
- The best example is main() method in Java. We write the main() method and fill it with some code but we never call it. It is automatically called by JVM (here, JVM is the software) to start the execution of an application.
- The five life cycle methods of Applet are init(), start(), paint(), stop() and destroy(). These methods are called by the Browser (here, the Browser is the software) whenever it thinks needed for the smooth execution of applet.
- run() method of thread. It is called implicitly whenever a thread starts its execution.
- service() method of Servlet. When the servlet is loaded by the Web container, it calls service() method automatically.
Advantages of Callback Methods
- They are called implicitly. Callback methods are written but not called by the Programmer explicitly like ordinary methods. They make programming simple.
- Programmer need not think when to call as the code or software decides when to call the callback method.