• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

brackets in the middle of a method call?

 
Matt Findley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...been reading up on and learning the Java Language recently and have been looking at a lot of example code. There is one bit of code structure in particular that loses me every time. I find it mostly in the examples provided in the Java tutorials on the Sun website. What is going on when a programmer puts a bunch of code in brackets {} that is inside the parentheses () of a method call? I simply can't follow it and can't find an explanation for this way of presenting code.

Here is an example excerpted from the sun site (http://java.sun.com/docs/books/tutorial/uiswing/learn/example1.html):

1 public static void main(String[] args) {
2 //Schedule a job for the event-dispatching thread:
3 //creating and showing this application's GUI.
4 javax.swing.SwingUtilities.invokeLater(new Runnable() {
5 public void run() {
6 createAndShowGUI();
7 }
8 });
9 }

What is going on here? The code at line 4 looks like it calls a method called "invokeLater", but what parameters does it pass to the method? All I see is instructions to create a new Runnable object followed by more instructions within two levels of brackets. Can someone walk me through the structure of this example so that it makes sense?

Thanks,

Matt Findley
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,

Welcome to JavaRanch!

What's happening here is that a new class is being defined, an instance of that class is being created, and it's being passed as an argument to a function, all at once! The class is called an anonymous inner class and in this case it's a class that implements the "Runnable" interface.



Here is the section of the Java Tutorial that talks about these and other kinds of nested classes.
 
Matt Findley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation and the pointer.

I felt redeemed by the part of their tutorial that says:

"Anonymous classes can make code difficult to read. You should limit their use to those classes that are very small (no more than a method or two) and whose use is well-understood (like the AWT event-handling adapter classes)."

It may have taken a while before I got to this part of the material.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

I have a doubt. It may sound silly, but pls do answer.

I have been working on Java for the past 1 year, but i never felt the
importance or necessity to create or use a anonymous/inner class.

Can you just explain me this with a simple and good example??


Thanx in advance,

Vijay
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Vaddem:
but i never felt the
importance or necessity to create or use a anonymous/inner class.


Hi Vijay,

I have found annonymous inner classes to be extremely important and useful while writing ActionListeners. Have you worked on Swings?
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, But only while learning Java...!

But not in real time. May be i may come across this concept in future projects.

Vijay
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem!

When you wish to write an ActionListener, annonymous inner classes are of great help. The syntax would be something as below:



This is only a sample example that I have included, but you will normally find yourself using annoynous inner classes (very frequently) while writing action listeners for your GUI components.

[ edited to break long line -ds ]
[ August 04, 2004: Message edited by: Dirk Schreckmann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic