Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What this code does?

 
Vivek Alampally
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
add(lbl);
}
});
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.

Thanks,
Vivek.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing please use CODETAGS while posting your queries.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic