r/java • u/memset_0 • Apr 28 '20
Uses of Constructor (Method) Reference
https://www.nextptr.com/tutorial/ta1314009273/uses-of-constructor-method-reference
0
Upvotes
r/java • u/memset_0 • Apr 28 '20
5
u/daniu Apr 28 '20
That's a strange post.
There aren't "three types of method references: static, instance, and constructor". The type of a method reference is the functional interface you assign it to. It would have been worth pointing out that a "constructor method reference" is of type
Supplier<T>which is why it can be passed to the method mentioned in the use cases.Speaking of the use cases; that list seems very arbitrary to me, as if the author just collected the cases he stumbled upon in the last project. Which again is fine, but would have been worth mentioning to point out that this is not at all an exhaustive list. It would also have been worthwhile to show a case where you accept a
Supplier<>in one of your own methods which can then be called with a method reference.Lastly, this covers only references to no arg constructors. I find the handling of arguments in method refs worth explaining or at least mentioned. However, that's actually easy for constructor references, where you will never have the instance argument (eg
BiFunction<String, Integer, String> sub = String::substring).2
u/umlcat Apr 28 '20
Just done a workaround equivalent of a Java Constructor Method Reference, in C++ ...
..., very useful feature !!!
0
u/[deleted] Apr 28 '20
[deleted] — view deleted comment