This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes brackets in the middle of a method call? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "brackets in the middle of a method call?" Watch "brackets in the middle of a method call?" New topic
Author

brackets in the middle of a method call?

Matt Findley
Greenhorn

Joined: Aug 02, 2004
Posts: 2
...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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Matt Findley
Greenhorn

Joined: Aug 02, 2004
Posts: 2
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

Joined: Feb 13, 2004
Posts: 243
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

Joined: Jul 04, 2004
Posts: 195
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?


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
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

Joined: Jul 04, 2004
Posts: 195
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: brackets in the middle of a method call?