Skip to content

Instantly share code, notes, and snippets.

@fluency03
Created June 29, 2017 09:25
Show Gist options
  • Select an option

  • Save fluency03/c294435157ca7f70678a782cb02bd8e7 to your computer and use it in GitHub Desktop.

Select an option

Save fluency03/c294435157ca7f70678a782cb02bd8e7 to your computer and use it in GitHub Desktop.
/**
* http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.13.1
*/
import java.util.ArrayList;
import java.util.List;
public class Foo {
public static void main(String[] args) {
List<Bar> l = new ArrayList<>();
l.add(new Bar());
l.add(new Bar());
l.add(new Bar());
l.stream()
.peek(Bar::print) // error: invalid method reference
// non-static method print() cannot be referenced from a static context
.forEach(Bar::print); // error: invalid method reference
// non-static method print() cannot be referenced from a static context
Bar b = new Bar();
b.print();
Bar.print(b);
}
}
class Bar {
public void print() {
System.out.println("this: " + this);
}
public static void print(Bar b) {
System.out.println("bar: " + b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment