Search code examples
javalambdafunctional-programmingjava-8functional-interface

Java 8 Supplier with arguments in the constructor


Why do suppliers only support no-arg constructors?

If the default constructor is present, I can do this:

create(Foo::new)

But if the only constructor takes a String, I have to do this:

create(() -> new Foo("hello"))

Solution

  • That's just a limitation of the method reference syntax -- that you can't pass in any of the arguments. It's just how the syntax works.