It's not a secret anymore!
The moose likes Java in General and the fly likes What this code does? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What this code does?" Watch "What this code does?" New topic

What this code does?

Vivek Alampally
Ranch Hand

Joined: Jul 10, 2008
Posts: 67
Hi everybody,
Can anyone here tell me what the following code does
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel("Hello World");
It looks strange. I can't understand the syntax. Could anyone please explain what is this. I know Thread can be created by extending Thread class/implementing runnable.
But strange part here is it is creating an object by newing a Runnable which is an interface.

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

here we are creating a annonymouse inner class which implement the runnable interface.

donot worry you will see such kind of syntax when you reach the innerclass chapter if you are studing for SCJP

SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

One more thing please use CODETAGS while posting your queries.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Vivek, run below code; this example is almost related to your question

the annonymous class allow you to override a method for a particular instance.
jQuery in Action, 3rd edition
subject: What this code does?
It's not a secret anymore!