Friday, February 29, 2008 07:59 PM
In a nutshell, "callback" methods are used to arrange for something you are doing in one area of the code to inform you that something has happened: progress being made, an error happening, a user selection, etc. In general, that little "something you are doing" is still in the process of executing. More abstractly, it is like a parenthetical thought (hey, like this!) added in the middle of a sentence.
In Java, this shows up most obviously in the UI event handling routines. You say: "when this button is pressed, I want this code to execute in response." In this case, you register an implementation of a particular interface (ActionListener's actionPerformed method) on the button in question. When the end-user clicks the button, that part of the code you don't control -- the event dispatching thread -- calls code in your callback method that you *do* control to do something interesting. Also, parsing XML using a SAX-based approach is ripe with callback methods as the parser lets you know what sort of node it has encountered while reading the file. And so forth.