I came across this example:
package br.com.teste;
class HighTemp {
private int hTemp;
HighTemp(int ht) {
hTemp = ht;
}
boolean sameTemp(HighTemp ht2) {
return hTemp == ht2.hTemp;
}
}
interface MyFunc152<T> {
boolean func(T v1, T v2);
}
class InstanceMethWithObjectRefDemo {
static <T> int counter(T[] vals, MyFunc152<T> f, T v) {
int count = 0;
for (int i = 0; i < vals.length; i++)
if (f.func(vals[i], v)) count++;
return count;
}
public static void main(String args[]) {
int count;
HighTemp[] weekDayHighs = { new HighTemp(89), new HighTemp(82), new HighTemp(90), new HighTemp(89) };
count = counter(weekDayHighs, HighTemp::sameTemp, new HighTemp(89));
System.out.println(count + " days had a same of 89");
}
}
Why this works? Particularly the part where an method reference is passed to a function with an interface argument.
count = counter(weekDayHighs, HighTemp::sameTemp, new HighTemp(89));
Why HighTemp::sameTemp is valid as MyFunc152 ? And why passing HighTemp::sameTemp does not generate a compile error if sameTemp is not static?