poeschlorn
poeschlorn

Reputation: 12440

Strange Java code: Class in a Class

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();
    }
};

Upvotes: 0

Views: 452

Answers (7)

Brandon Coffman
Brandon Coffman

Reputation: 244

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.

Upvotes: 2

Michał Mech
Michał Mech

Reputation: 2055

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.

Upvotes: 1

fortran
fortran

Reputation: 76067

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.

Upvotes: 1

Thomas
Thomas

Reputation: 8013

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.

Upvotes: 2

ZeissS
ZeissS

Reputation: 12135

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).

Upvotes: 6

Winston Chen
Winston Chen

Reputation: 6879

Yeah, this is called: anonymous class.

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

Upvotes: 1

Juha Syrj&#228;l&#228;
Juha Syrj&#228;l&#228;

Reputation: 34271

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.

Upvotes: 4

Related Questions