Callback methods

  • Thread starter Dilantha Seneviratne
  • Start date
D

Dilantha Seneviratne

Hi
Could someone advise me what a callback method is, when and how they
are developed and used.

thanks
 
J

Joona I Palaste

Dilantha Seneviratne said:
Hi
Could someone advise me what a callback method is, when and how they
are developed and used.

AFAIK a callback method is a method you write in your own code, but is
called from someone else's code. It is typically used when you are using
a component from your code. The component processes some data, then
calls a callback method. What that callback method does is up to you to
decide. Typically you write a class implementing some interface and
give it as a parameter to the component.
 
J

Joe Smith

Dilantha Seneviratne said:
Hi
Could someone advise me what a callback method is, when and how they
are developed and used.

thanks

One example of callback:

You ask a component to do a long lasting operation, and you pass it a
reference to you ("this"), so it can call one of your methods when it has
finished (so you can retrieve the results, update the states, etc).
 
E

Eric Sosman

Joona said:
AFAIK a callback method is a method you write in your own code, but is
called from someone else's code. It is typically used when you are using
a component from your code. The component processes some data, then
calls a callback method. What that callback method does is up to you to
decide. Typically you write a class implementing some interface and
give it as a parameter to the component.

As another example, consider defining a class whose
objects will be inserted in a HashMap. You must provide
hashCode() and equals() methods for your class (or inherit
them from the parent class), and when you perform operations
on the HashMap it "calls back" to the hashCode() and equals()
methods of your objects.
 
A

Anthony Borla

Dilantha Seneviratne said:
Hi

Could someone advise me what a callback method is,
when and how they are developed and used.

A callback is a method that is executed by another method that has a
reference to the callback [in languages that support method pointers], or
its owning object. Keeping a reference in this way allows the caller to
execute [or 'call back'] the callback at some indeterminate future time.

A very simple example follows:

class Callee
{
...
void execute() { ... }
...
}

class Caller
{
...
public Caller(Callee cb) { this.cb = cb; }
...
void makeTheCallback() { cb.execute(); }
...
private cb;
}

...
public static void main(String[] args)
{
Caller ca = new Caller(new Callee());
...
if (...)
ca.makeTheCallback();
...
}

Probably the most common example of a callback in Java is the use of event
listeners in Swing. The programmer creates the callback / callee, registers
it with the relevant component, and the system 'calls it back' to handle the
event for which it has been registered [note this is a very rough
description, but should be enough to give you an idea].

I hope this helps.

Anthony Borla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top