I see this notation, a new operator with a class name and then bracketed code, occasionally in Android examples. Can someone explain this? In the example below, PanChangeListener is a class (or maybe an interface) and ‘new’ creates an instance, but what role does the bracketed code play with respect to the PanChangeListener?
fType pcListener = new PanChangeListener() {
@Override
public void onPan(GeoPoint old, GeoPoint current) {
//TODO
}
});
Even a name for this syntax would be useful, as I could Google it.
That’s an anonymous class.
The syntax allows you to create a new class, provide an implementation for some methods, then instantiate it.
It works in a similar way to the following code that doesn’t use an anonymous class: