Skip to content

Instantly share code, notes, and snippets.

@hishidama
Last active July 13, 2017 02:54
Show Gist options
  • Save hishidama/2b5dda47809333d0763c7a63e3145213 to your computer and use it in GitHub Desktop.
Save hishidama/2b5dda47809333d0763c7a63e3145213 to your computer and use it in GitHub Desktop.
メソッド参照できる・できない
package example;
// https://ideone.com/cx8hsF
public class MethodReferenceExample {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
Test test11 = String::trim;
// Test test12 = Hogehoge::trim; // NG
Hogehoge h = new Hogehoge();
Test test13 = h::trim;
Test test14 = Hogehoge::staticTrim;
// Test2 test21 = String::trim; // NG
Test2 test22 = Hogehoge::trim;
}
}
class Hogehoge {
String trim(String v) {
return v.trim();
}
static String staticTrim(String v) {
return v.trim();
}
}
interface Test {
String doSomething(String v);
}
interface Test2 {
String doSomething(Hogehoge hh, String v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment