Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p>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 <a href="http://functionaljava.org/" rel="nofollow noreferrer">Functional Java</a>:</p> <pre><code>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&lt;String&gt; a = array("Hello", "There", "what", "DAY", "iS", "iT"); final boolean b = a.exists(new F&lt;String, Boolean&gt;() { public Boolean f(final String s) { return fromString(s).forall(isLowerCase); } }); System.out.println(b); // true ("what" provides the only example; try removing it) } } </code></pre> <p>Instead of a <code>View.OnClickListener</code> you create a new <code>F</code>, which has an apply method called <code>f</code> instead of <code>onClick()</code>.</p> <p>See also <a href="https://stackoverflow.com/questions/1267297/functional-programming-in-java">Functional Programming in Java</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload