Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public static void main(String[] args) {
  2. File[] files = new File("C:").listFiles(File::isHidden); // OK
  3. test(MyCass::mymethod); // Cannot make a static reference to the non-static method
  4. }
  5.  
  6. static interface FunctionalInterface{
  7. boolean function(String file);
  8. }
  9.  
  10. class MyCass{
  11. boolean mymethod(String input){
  12. return true;
  13. }
  14. }
  15.  
  16. // HELPER
  17. public static void test(FunctionalInterface functionalInterface){}
  18.  
  19. listFiles(File::isHidden)
  20.  
  21. listFiles(f -> f.isHidden())
  22.  
  23. MyCass myCass = new MyCass(); // the instance
  24. test(myCass::mymethod); // pass a non-static method reference
  25.  
  26. test(new MyCass()::mymethod);
  27.  
  28. class MyCass{
  29. static boolean mymethod(String input){
  30. return true;
  31. }
  32. }
  33.  
  34. new FunctionalInterface{
  35. boolean function(String file){
  36. return MyClass.mymethod(file);
  37. }
  38. }
  39.  
  40. new FunctionalInterface{
  41. boolean function(String file){
  42. return _missing_object_.mymethod(); # mymethod is not static
  43. }
  44. }
  45.  
  46. test(MyCass::mymethod); // Cannot make a static reference to the non-static method
  47.  
  48. test(v -> MyCass.mymethod(v)); // static access
  49.  
  50. class MyCass {
  51. static boolean mymethod(String input) {
  52. return true;
  53. }
  54. }
  55.  
  56. public static void main(String[] args) {
  57. MyCass myCass = new MyCass();
  58. test(myCass::mymethod);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement