views:

160

answers:

5

Hi All

I new to java so bear with me if this is a ridiculously simple question but I am curious about this method call which has {code} being taken in - see code below for an example in the method addSelectionListener. What is the purpose of this? I have been looking through docs for an explaination but cant seem to find what this practice is called never mind any useful information.

    setStatusLine.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {

            String message = "I would like to say hello to you.";
            if (pressed) {
                message = "Thank you for using me";
            }
            setStatusLine(message);
            pressed = !pressed;
        }
    });

Thanks for any help or insights that can be offered

+6  A: 

this is an Anonymous Class, or Anonymous inner class. If you google for that you will find some tutorials/examples. Sun has some docs too.

Sam Holder
Thanks for the help guys, much appreciated.
+2  A: 

There is not a method call in fact... This code set a selection listener on the setStatusLine component.

An equivalent of this code could be

public class X implements SelectionListener{

    //In the constructor or an other method.
    setStatusLine.addSelectionListener(this);

    public void widgetSelected(SelectionEvent e) {

        String message = "I would like to say hello to you.";
        if (pressed) {
            message = "Thank you for using me";
        }
        setStatusLine(message);
        pressed = !pressed;
    }

}
Fred
+3  A: 

The method addSelectionListener receives a SelectionListener instance. It doesn't receive "code". The confusing thing is the use of new <class/interface name>(){...}. This construct is used for anonymous inner classes. In fact what the code above does is extending the SelectionAdapter class, overriding its widgetSelected method, creating an instance of the new class and passing it to addSelectionListener().

Usage of anonymous inner classes is common with listeners, where we create a new class, to be used in one specific place. Therefore we don't give it a name, and we prefer implementing it directly in the context where it is being used.

Eyal Schneider
+4  A: 

As the other contributors already said: It is an Anonymous Class

You could have created a new class named MyClass in a new file called McClass.java looking like that:

class MyClass extends SelectionAdapter {

  public void widgetSelected(SelectionEvent e) {
    <your code that's being executed whenever the widget is being selected>
  }

}

Then you could have changed the first line like that:

setStatusLine.addSelectionListener(new MyClass());

See? Now you have an "explicit" class with just one function. Often that is too much overhead and would clutter your design.

Does that help?

das_weezul
A: 

It took me some time to understand Anonymous Inner classes. The basic things to remember are:

They are just like parameters, except instead of passing in an primitive or Object you pass in a class that implements an Interface/extends a class (yes they also work with interfaces) depending on method parameter. They are anonymous, so "disappear" right after the method has popped off the stack. }); is a dead give-away for an anonymous inner class. They often pop-up in user interfaces for listener events They save clutter in your code, but also make it harder to read.

For full punishment read the JLS: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9.5

If you are interested in knowing the nitty gritty about such things, reading the SCJP book and doing the exam is good or you can study the JLS. It won't learn you how to code, but it will help you understand how Java, and in some way, many other OO languages work.

Selenia