Instantiating Java object with a passed in method

Posted by superluminary on Stack Overflow See other posts from Stack Overflow or by superluminary
Published on 2012-06-12T10:26:37Z Indexed on 2012/06/12 10:40 UTC
Read the original article Hit count: 141

It's been a few years since I've been heavily into Java. Coming back to it I'm seeing this pattern all over the place:

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
  public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
    // do work
  }
});

This looks more like Functional programming to me. It's a nice pattern but how is it possible to pass a method like this? In the old days a class was a class, and once compiled there was little you could do to it.

My questions are:

  1. Can anyone let me know what this pattern is called?
  2. How can I write a class that can be instantiated in this way.
  3. Are there any other useful examples of functional patterns that have made their way into Java?
  4. What do I need to Google to read more about this?

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about functional-programming