I'm following this Swing tutorial and I ran across this snippet of code:
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SimpleExample ex = new SimpleExample();
ex.setVisible(true);
}
});
What's happening inside the EventQueue.invokeLater
params?
invokeLater
expects an argument of typeRunnable
, i.e. an instance of a class implementing theRunnable
interface. In this example, this method receives an instance of an anonymous class that implementsRunnable
.Runnable
only has a single method -run
- so all the anonymous class instance has to implement is thatrun
method.In Java 8, there's an even shorter syntax, using a lambda expression :