SwingUtilities method param contains entire method body?

Matthew :

I've been reading over the SwingUtilities.invokeLater() method, and I understand in large part its purpose. When dealing with GUI components, all actions on these components need to be done on the same Event thread to avoid races, etc.

However, and what I think many questions have failed to expand on, is how the code actually works? Looking at it:

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        //method body code
    }
);

The question I have is, how is this working? We are passing the invokeLater() method a new Runnable obj (which I thought Runnable was an interface anyway, how are we instantiating a constructor new Runnable()?) and then attaching a whole method body with it inside a method call: {//Method body code});. How is that possible? I've never seen a method take in an entire method body as one of its parameters.

When I try to replicate this with my own methods, I get errors indicating that I cannot supply entire method bodies within a method parameter. Thus, I come back to my initial question of how this actually works. Perhaps I am overthinking/overlooking, but any help is greatly appreciated.

Thank you.

Elliott Frisch :

Anonymous Classes. You can pass any class that implements Runnable, even an anonymous one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87949&siteId=1