callback : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

callback
Java does not have procedure variables to implement function pointer callbacks the way C/C++ does. A callback would be used in C for example to pass a sin or cos function to generic plotting program. Callbacks are a way of passing functions as parameters. The callee can call the function passed to it as a parameter as often as it wants, with whatever parameters it wants.

Why would you want a callback? The normal parameter passing mechanism lets caller provide the callee with information, but provides only meager flow of information in the reverse direction, the single returned result primitive or Object. Further, the callee cannot provide further information back to the caller once it has returned. The callback mechanism allows a two-way flow of information and exchange of computing resources between caller and callee.

Consider a component consisting of a panel of buttons. The caller wants to control the labelings on the buttons, how they are rendered etc. The callee wants to notify the caller whenever one of the buttons was pressed and which one. The callback mechanism, sometimes called a Listener mechanism, allows the callee to notify the caller, the listener, of interesting events, or to use some of the methods the caller has access to.

There are four basic techniques to fake a callback function in Java.

  1. Subclassing. Subclass the calling class with an overridden callback method. Pass this as a parameter.
  2. Delegation via subclassing. Pass a subclassed Callback delegate object that knows how to invoke the callback method(s).
  3. Pass a Callback delegate object that implements an interface for the callback method(s).
    HeapSort: source code for an example of the delegate technique
    This is how it is most commonly done. It offers most flexibility,
  4. Use inner classes to define an anonymous callback class, instantiate an anonymous callback delegate object and pass it as a parameter all in one line. This technique with EventListeners tends to muddle GUI (Graphic User Interface) and program logic making your GUI code non-reusable. Unless the anonymous inner classes are simple, it is generally wiser to make them ordinary free-standing named classes.
If you don’t already understand delegates, that probably sounds like Greek. Just look at the HeapSort example. Once you understand that code, you will easily understand the other three variants. It is much simpler than it first sounds.

Callback has a totaly different meaning in the context of security. For secure login you call the server. It then calls you back. This makes it harder for you to spoof being someone else. This may be done over phone lines or over the Internet. It is the same scheme a jealous husband might use to ensure his wife is really home as she claims.


This page is posted
on the web at:

http://mindprod.com/jgloss/callback.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\callback.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.17.179.132]
You are visitor number