• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Callback functions.

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading chapter 6 Interfaces and Inner classes from Core.Java.Volume-I.Fundamentals 8Edition by Cay S. Horstmann and Gary Cornell.

In the chapter I came across section of function callbacks.

There is an example give for that in which I have a doubt.




Now in above code there is no explicit call to "actionPerformed(ActionEvent event)" method in class TimePrinter.

So does that mean compiler automatically inserts a call and that is what a "callback" function means ?

Can anybody help me with this?

Thanks in advance.




 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not exactly. Put yourself in the shoes of the Timer class. You are passing it an object whose interface is ActionListener. The Timer knows that, when the waiting time is over, it will have to call the only method of the ActionListener interface, which is actionPerformed.
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are passing it an object whose interface is ActionListener. The Timer knows that, when the waiting time is over, it will have to call the only method of the ActionListener interface, which is actionPerformed.



So Christophe Verré do you mean to say that passing a reference ti Timer constructor has achieved the callback process??

Please correct me if I am wrong.

Actually I am trying to find an exact meaning of callback function practically.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a definition of a callback.

A callback is something that you do not call explicitly. Like here, you have defined some functionality (print a message and beep), which will be called, not by you, but by some other entity. In your case, by a timer instance.

It's the same for buttons in Swing. You can define an action which will be called when a button is pressed. You won't call the action yourself. Swing will call it for you, when you press the button.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Timer class performs the following function

it takes two arguments.

The first one says the number of milliseconds for which you want an exception(in this case an action) to be thrown. The second argument is the one which handles that action.

After 10 seconds the timer class throws an event(an action, or an exception or an interrupt any thing you wish) which is caught by the listener class and processed.

I hope this helps
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to PrasannaKumar Sathiyanantham and Christophe Verré.

My doubt is cleared now.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic