Skip to content

Instantly share code, notes, and snippets.

@thieux
Created June 26, 2016 22:51
Show Gist options
  • Save thieux/18aa7e96ce9687de0a26eea9275cbb07 to your computer and use it in GitHub Desktop.
Save thieux/18aa7e96ce9687de0a26eea9275cbb07 to your computer and use it in GitHub Desktop.
Java8 method reference
class Java8MethodRef {
interface Equality1 {
boolean eq(Object a);
}
interface Equality2 {
boolean eq(Object a, Object b);
}
interface Equality3 {
boolean eq(Object a, Object b, Object c);
}
public void compatibleTypes() {
Equality2 a = Object::equals;
}
public void incompatibleTypes() {
// invalid method reference
// non-static method equals(java.lang.Object) cannot be referenced from a static context
Equality1 e1 = Object::equals;
// incompatible types: invalid method reference
// method equals in class java.lang.Object cannot be applied to given types
// required: java.lang.Object
// found: java.lang.Object,java.lang.Object,java.lang.Object
// reason: actual and formal argument lists differ in length
Equality3 e = Object::equals;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment