1
package com.java8;

public class MethodReferenceExample {

    public MethodReferenceExample() {
        System.out.println("MethodReferenceExample.....");
    }

    public int display() {
        System.out.println("in display");
        return 1;
    }

    public static int read() {
        System.out.println("in read");
        return 10;
    }

    public static void main(String[] args) {
        //point 1
        MyInterface myIn = MethodReferenceExample::new;
        myIn.show();

        //point 2
        MyInterface objectReference = new MethodReferenceExample()::display;
        objectReference.show();
        int value = objectReference.show(); //you can't assign this because show is void///

        MyInterface staticReference = MethodReferenceExample::read;
        staticReference.show();


    }

}


interface MyInterface{
    public void show();
    default public int getvalue() {
        return 10;
    }
}

In this at point 1 we can assign constructor reference using ::new but that interface is not implementing that

In point 2, we can call display method which has return value as int using show method which has return value as void and still it calls that method. How weird is this? There's no connection between 2 methods still it can call that because it has same number of arguments. Why has java people made this change? I mean, this method reference is really confusing and really makes no sense, no valid justification then why have they introduced this confusing change?

Again in point 2 we can call that but can't assign return value to int because calling method is void so it can't convert void to int, seriously why all this confusion? Can someone explain me this? It's pretty hard for my brain to accept all this method reference thing.

Thanks in advance.

1
  • That is the beauty of method reference, you just need to match the signature. Commented Mar 24, 2019 at 7:36

1 Answer 1

3

In point 2, we can call display method which has return value as int using show method which has return value as void and still it calls that method. How weird is this?

Just as weird as being able to call, for example, someList.add(something) and ignore the returned value, just as if the return type of List.add() was void. It's not void: it's boolean, but you don't care about the return value, so you call it as if it was returning void and just ignore the result. Same here. The interface defines a method that you can call without any argument. display() is such a method. As simple as that.

Why has java people made this change?

Because it's a good change that often makes code clearer, and more efficient too.

this method reference is really confusing and really makes no sense

It does make a lot of sense, and has plenty of justifications. The fact that you don't yet understand them doesn't make them less useful.

Sign up to request clarification or add additional context in comments.

1 Comment

I liked Ur first example of list.add. U said, "It does make a lot of sense, and has plenty of justifications." So can you please give those examples? I am not asking this question for cursing some API but because I want to really understand it's importance which I have not understood fully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.