---
# System prepended metadata

title: Method Reference
tags: [ocpjp]

---

# Method Reference
[參考網站](http://zyzhang.github.io/blog/2013/06/15/java8-preview-method-reference/)

```java=
class Product{
	String name;
	int qty;
	public String toString() {
		return name;
	}
	public Product(String name,int qty) {
		this.name = name;
		this.qty = qty;
	}
	static class ProductFilter{
		public boolean isAvailable(Product p) {
			return p.qty >= 10;
		}
	}
}
public class Test {

	public static void main(String[] args) {
		List<Product> products = Arrays.asList(new Product("MotherBoard",5),new Product("Speaker",20));
		products.stream().filter(Product.ProductFilter::isAvailable).forEach(p->System.out.println(p));
	}
}
```

## Which modification enables the code fragment to print Speaker?
A. Implement Predicate in the Product.ProductFilter class and replace line n2 with .filter (p ->
p.ProductFilter.test (p))
**B. Replace line n1 with:
public static boolean isAvailable (Product p) {**
C. Replace line n2 with:
.filter (p -> p.ProductFilter: :isAvailable (p))
D. Replace line n2 with:
.filter (p -> Product: :ProductFilter: :isAvailable ())
- [x] **Answer: B**

###### tags: `ocpjp`