Sunteți pe pagina 1din 2

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.

Callback using Interfaces in Java


In Simple Words... In plain English, a callback function is like a Worker who "calls back" to his
Manager when he has completed a Task.

In the case of Event-driven programming, we pass a reference to a function which will get called
when an event occurs. This mechanism is termed as a callback. Java does not support function
pointers. So we can not implement the same direction. But using interfaces we can achieve the
same very easily.
In the example below, we've made a callback when a button is clicked. See the steps:
1. Create an interface ClickEventHandler with a single method handleClick().
2. Create a ClickHandler class which implements this interface ClickEventHandler.
3. Create a Button class which will call ClickHandler when it's click method is called.
4. Test the application.
Example
Live Demo
//Step 1: Create an interface for the callback method
interface ClickEventHandler {
public void handleClick();
}

//Step 2: Create a callback handler


//implementing the above interface
class ClickHandler implements ClickEventHandler {
public void handleClick() {
System.out.println("Clicked");
}
}

//Step 3: Create event generator class


class Button {
public void onClick(ClickEventHandler clickHandler) {
clickHandler.handleClick();
}
}

public class Tester {


public static void main(String[] args) {
Button button = new Button();
ClickHandler clickHandler = new ClickHandler();
//pass the clickHandler to do the default operation
button.onClick(clickHandler);

Button button1 = new Button();


//pass the interface to implement own operation
button1.onClick(new ClickEventHandler() {
@Override
public void handleClick() {
System.out.println("Button Clicked");
}
});
}
}

S-ar putea să vă placă și