• Post Reply Bookmark Topic Watch Topic
  • New Topic

What kind of syntax is this?  RSS feed

 
David Tyson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm new to Java and am flicking through Eckel's Thinking in Java book. I was reading the graphical user interfaces section and came across some unfamiliar syntax written in the 'main' method:



My interpretation is:

invokeLater is a method of the static class, SwingUtilities
invokeLater takes a Runnable object as an argument

But what is happening after that? My very hesitant guess is that a new class that implements the Runnable interface is created and defined on the spot, but how come the class doesn't have a name? And how come there isn't an 'implements' keyword anywhere? And why is there a '()' after 'Runnable'?

Can someone explain what is going on? I didn't know classes could be created within methods.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Tyson wrote:Hi guys, I'm new to Java and am flicking through Eckel's Thinking in Java book. I was reading the graphical user interfaces section and came across some unfamiliar syntax written in the 'main' method:



My interpretation is:

invokeLater is a method of the static class, SwingUtilities
invokeLater takes a Runnable object as an argument

But what is happening after that? My very hesitant guess is that a new class that implements the Runnable interface is created and defined on the spot, but how come the class doesn't have a name? And how come there isn't an 'implements' keyword anywhere? And why is there a '()' after 'Runnable'?

Can someone explain what is going on? I didn't know classes could be created within methods.


That class without a name is an anonymous inner class. And you can read all about it in the Java tutorial ...

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New class which implements the Runnable interface, he says?

There are few people who could have worked that out by reading that code. Well done
 
David Tyson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for telling me what that is, Henry. I couldn't find anything on it in the link you gave, but this page was helpful: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

Especially this:

3.12.3. New Syntax for Anonymous Classes

We've already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as:

new class-name ( [ argument-list ] ) { class-body }
or:

new interface-name () { class-body }


Which clears up the '()' and lack of 'implements' when defining the class that I was confused about.

Haha Campbell, I only knew about that because I had read a little bit about interfaces (and how you need to use 'implements' keyword) earlier and I knew that Runnable was an interface.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still an achievement working that out from reading the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!