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.