1

I am trying to better understand how method references work. In this example, I follow the logic of the code, but I don't understand the value of doing this. Basically, someMethod() is used as the implementation of the display() abstract method of MyInterface after the method reference assignment in STEP 2. But why? What are we gaining by doing this? When does this make sense architecturally? Seems that we take the Interface that has nothing to do with the class and associate them with this assignment.

@FunctionalInterface
interface MyInterface {
       void display();
}

class DerivClass {
    public void someMethod(){  
            System.out.println("Derived class method");  
    }
}

public class RefTest {

    public static void main(String[] args) {
        DerivClass dc = new DerivClass(); // STEP1: class instance
        MyInterface myInterf = dc::someMethod; // STEP 2: assign method ref to interface    
        myInterf.display(); // STEP 3: executes someMethod(), prints "Derived class method"
    }
}
user1385969
  • 195
  • 2
  • 2
  • 11
  • 1
    My recommendation is that you learn about functional programming and the power of high order functions. That's the key to understand all this. – Edwin Dalorzo Oct 13 '20 at 16:59

1 Answers1

9

Good place you to start understanding the method reference is the Oracle Documentation, that says:

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.

Think of it this way:

"Method definition (particular body, signature and a return type) exists; why shall I create a new lambda expression if I can reuse existing code? let's refer to the already defined method and reuse it."


Four kinds of method references:

  1. SomeType::staticMethodName - reference to the static method (you just refer to the static method of "SomeType" class);

  2. someTypeInstance::instanceMethodName - reference to the instance method of a particular object (here, you need an actual object, from which you obtain the method reference);

  3. SomeType::instanceMethod - reference to the instance method of an arbitrary object of a particular type (you do not need to explicitly create an object in this case);

  4. SomeType::new - reference to the constructor (you simply refer to the constructor).


Difference between (2) the particular object's method reference and (3) arbitrary object's method reference:

In the first two examples, the method reference is equivalent to a lambda expression that supplies the parameters of the method. For example:

System.out::println == x -> System.out.println(x);
Math::pow == (x, y) -> Math.pow(x, y)

In the third case, the first parameter becomes the target of the method, as in:

String::compareToIgnoreCase == (x, y) -> x.compareToIgnoreCase(y)
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66