Java Reference
In-Depth Information
To check your understanding of method and constructor references, try out Quiz 3.7 .
Quiz 3.7: Constructor references
You saw how to transform zero-, one-, and two-argument constructors into constructor
references. What would you need to do in order to use a constructor reference for a
three-argument constructor such as Color(int, int, int)?
Answer:
You saw that the syntax for a constructor reference is ClassName::new, so in this case it's
Color::new. But you need a functional interface that will match the signature of that constructor
reference. Because there isn't one in the functional interface starter set, you can create your
own:
public interface TriFunction<T, U, V, R>{
R apply(T t, U u, V v);
}
And you can now use the constructor reference as follows:
TriFunction<Integer, Integer, Integer, Color> colorFactory = Color::new;
We've gone through a lot of new information: lambdas, functional interfaces, and method
references. We put it all into practice in the next section!
Search WWH ::




Custom Search