As the others point, out, this is an anonymous inner class. It's syntactic shorthand that creates a new subclass of View.OnClickListener, one that overrides the onClick() method with the behavior you want.
Your intuition that this is related to a C# lambda expression is fairly accurate. Although Java doesn't have lambdas (yet), you can simulate one by creating an object with an 'apply' method and passing around a reference to it. This technique used in functional programming libraries for Java. Here's an example from Functional Java:
import fj.F;
import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static fj.function.Characters.isLowerCase;
public final class Array_exists {
public static void main(final String[] args) {
final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
final boolean b = a.exists(new F<String, Boolean>() {
public Boolean f(final String s) {
return fromString(s).forall(isLowerCase);
}
});
System.out.println(b); // true ("what" provides the only example; try removing it)
}
}
Instead of a View.OnClickListener
you create a new F
, which has an apply method called f
instead of onClick()
.
See also Functional Programming in Java