views:

232

answers:

7

In some sample codes there are methods and classes declared WITHIN other methods and/or classes.

I've never heard/read about this. What effect does this kind of programming have? Wouldn't it be better to write down classes in a seperate file and methods side by side and not within each other (like every book tells you)? What are the advantages and disadvantages of this kind of programming?

Here's an example of what I mean:

Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        TextView textView = (TextView) findViewById(R.id.description);
        textView.setText(mRoad.mName + " " + mRoad.mDescription);
        MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    }
};
+3  A: 

That is called anonymous inner class. Suppose you will use the class only in a single place and the class is short and simple, then there may be no point to create a public class.

This page contains more information about anonymous inner classes.

Juha Syrjälä
+1  A: 

Yeah, this is called: anonymous class.

You can read more about it here: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

Winston Chen
+5  A: 

There are two types of classes that can be in a class: Inner Classes and Anonymous Classes.

Both are mainly used to create classes that do not work alone but need access to the surrounding object. These classes have full access to the whole surrounding object (exception: A definition of a static inner class).

ZeissS
Thx for the quick answers...but one question is left open...why declaring a method (like in the code on top), when it is only used once? Is there a good reason for it?
poeschlorn
Well, take a look at the other code. The handler is surrely as a bridge between the current class and the event system. This way, the current class does not need to extend or implement the Handler class/interface.
ZeissS
+2  A: 

Here is a good read on anonymous classes and why they can be useful. It is mostly used in scenarios where the implementation of the class is quite short and not re-used anywhere else. By using this anonymous implementation you can keep the code short and concise.

Thomas
+1  A: 

The inner classes also have the advantage of being able to reference the container class without having an explicit pointer.

Being anonymous is just a matter of conciseness.

fortran
+1  A: 

One of commons example is Listener implementation:

class myPanel extends JPanel {
    public MyPanel() {
        JButton b1 = new JButton("Hello");
        b1.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // do something for button b1
                }
            }
        );
    }
}

Now imagine that the Listener implementation can depends on some outer class (myPanel) properties. Using anonymous class You can easily do this.

Michał Mech
+2  A: 

Anonymous classes are common when defining a small implementation of an interface you are working with, such as EventListeners. One thing to note about them is that you will NOT have access to local variables (and parameters) unless they are declared final.

loliii