Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, How does the compiler know which constructor to call in this lambda expression

Tags:

java

I have a question, I'm learning java with a Book, and as I've copied some code in there (and made some changes) and made some investigations, I noticed something odd..., here is the code

public static void main(String[] args)
{
    Timer timer = new Timer(1000, (event) ->
    {
        System.out.println("At the Tone, the time is" + Instant.ofEpochMilli(event.getWhen()));
        Toolkit.getDefaultToolkit().beep();
    });

    timer.start();
    JOptionPane.showMessageDialog(null, "Quit?");
    System.exit(0);

}

It's just a code that notifies you if a second passes. (this code compiles and runs smoothly)

as you can see Timer Constructor requires 2 pars (int, ActionListener)

public Timer(int delay, ActionListener listener)

and the ActionListener Interface has one method that is actionPerformed and requires ActionEvent parameter

public void actionPerformed(ActionEvent e);

Now here is my question, when calling this actionPerformed Method in that lambda expression above How does the compiler know which constructor to call to instantiate ActionEvent without feeding him any clues about the parameters, ActionEvent has no "No argument Constructor" and the method getWhen() is not static (obj must be instantiated)

Here are all the constructors of ActionEvent:

public ActionEvent(Object source, int id, String command)

public ActionEvent(Object source, int id, String command, int modifiers)

public ActionEvent(Object source, int id, String command, long when,
                   int modifiers)

I really hope I made myself clear!, Thank you

like image 713
Naito Avatar asked Nov 29 '25 16:11

Naito


1 Answers

The compiler does not know! ;-)
It is the Timer instance that will create the ActionEvent instance at runtime every delay and the Timer instance will call the ActionListener method actionPerformed with the ActionEvent it created.

You can have a look at one of the implementation here:
http://developer.classpath.org/doc/javax/swing/Timer-source.html

/**
 * Fire the action event, named "Timer" and having the numeric
 * identifier, equal to the numer of events that have been
 * already fired before.
 */
void fireActionPerformed()
{
  fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
}
like image 121
grandouassou Avatar answered Dec 02 '25 05:12

grandouassou