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