Java Made Clear: Callback Methods and Advantages

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

  1. 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.
  2. 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.
  3. run() method of thread. It is called implicitly whenever a thread starts its execution.
  4. service() method of Servlet. When the servlet is loaded by the Web container, it calls service() method automatically.

Advantages of Callback Methods

  1. They are called implicitly. Callback methods are written but not called by the Programmer explicitly like ordinary methods. They make programming simple.
  2. Programmer need not think when to call as the code or software decides when to call the callback method.

Some questions for you from java.lang package.

Leave a Comment

Your email address will not be published.