I have often come across this snippet :
General form of invokeLater is – static void invokeLater(Runnable obj)
But i have often come across these types of codes:
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new tester();
}
});
}
or—–(example of another type)
{
clean.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cleanActionPerformed(ae);
}
});
}
Now what is this?
There is method in the constructor!! I am unable to understand the way of writing these snippets.
Explain very clearly
What you see are anonymous inner classes. They are one-shot classes that you won’t use anywhere else.
Take a look at this piece:
addActionListeneris expecting an implementation of an ActionListener, which is an interface.Now, you could write a whole new class that implements it and then put an instance of it as an argument of
addActionListener, but this way is faster to write (and a bit harder to read) if you won’t use such a class anywhere else.The same effect could have been achieved with this: