class Main {
public static void main(String[] args) {
I i1 = A::m;
}
@FunctionalInterface
interface I{
void method();
}
class A {
public static void m() {}
}
}
Last edited by admin on Thu Apr 18, 2024 8:40 am, edited 1 time in total.
Reason:Please put code inside [code] [/code]
Reference to an Instance Method of an Arbitrary Object of a Particular Type
The following is an example of a reference to an instance method of an arbitrary object of a particular type:
String[] stringArray = { "Barbara", "James", "Mary", "John",
"Patricia", "Robert", "Michael", "Linda" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).
Similarly, the method reference String::concat would invoke the method a.concat(b).
So we have Tiger::eat would invoke the method t.eat(List<String> foods)
~because Carnivore have 2 parameter (Tiger t, List<String> foods)
class Test {
public static void main(String[] args) {
Function<Person, Integer> f = Person::getAge;
f.apply(new Person());
}
}
class Person {
private int age;
public int getAge() {
return age;
}
}